+ 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

17th May 2017, 8:47 PM
hamid
hamid - avatar
6 Réponses
+ 8
this is a good question and info 👍
26th May 2017, 9:25 AM
NimWing Yuan
NimWing Yuan - avatar
+ 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; }
18th May 2017, 2:14 AM
Rrestoring faith
Rrestoring faith - avatar
+ 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?
18th May 2017, 3:21 PM
Rrestoring faith
Rrestoring faith - avatar
+ 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
18th May 2017, 9:54 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
can you tell me more?
18th May 2017, 8:24 AM
hamid
hamid - avatar
+ 1
exactly thats my problem, but make an example please(about override toString)
18th May 2017, 9:47 PM
hamid
hamid - avatar