+ 2
Write arraylist to a file and read it.
I have teacher class with string name and int code, in main class define a new arrylist of teachers (arraylist<teacher> teachers). when I want save this arraylist to a file with bufferedstream and ... I dont have teachers names and codes and my file has contents like this: [mynewpackage.teacher@1db9742,mynewpackage.teacher@106d74c] why? please help me
6 Respuestas
+ 8
this is a good question and info 👍
+ 6
If you are trying to print the teacher object you need to Override the toString method in your teacher object. It's giving you the reference in memory of the object.
@Override
public String toString(){
return this.teacherName;
}
+ 5
From your output @1db9742
This is the location in memory of the object. This is what objects will output if you try to print them.
Ex/
Animal dog = new Animal();
System.out.println(dog);
If you want to print something like the members of the class you need to Override toString. This will change the way the object is printed. Unfortunately if this isn't your issue, I'm not sure what is, from reading the question. Have you successfully printed something onto the file?
+ 5
Refer to my first post. That function is what it would look like (an example). It just needs to be in the Teacher class.
Here's another example just encase:
class Dog{
private String name = "joe";
@Override
public String toString(){
return "This a dog, his name is "+name;
}
}
Then if I were to write:
Dog adog = new Dog();
System.out.println(adog);
Output:
This a dog, his name is joe
+ 1
can you tell me more?
+ 1
exactly thats my problem, but make an example please(about override toString)