I am creating an array list full of students details that needs to be written to a file but when it writes to a file it is unreadable data. such as [javaapplication2.student@15db9742]
private void aList() throws IOException {
ArrayList<student> enrolledStudents = new ArrayList<student>();
student James = new student("James", "23 Old Drive", 220298, "Male");
enrolledStudents.add(James);
student Matt = new student("Matt", "13 Wee Park", 110502, "Male");
enrolledStudents.add(Matt);
student Gary = new student("Gary", "1 Green Close", 240897, "Male");
enrolledStudents.add(Gary);
student Charlotte = new student("Charlotte", "5 Park Link", 230402, "Female");
enrolledStudents.add(Charlotte);
student Naomi = new student("Naomi", "133 Caulside", 180801, "Female");
enrolledStudents.add(Naomi);
student Ryan = new student("Ryan", "88 Wood Burn", 100602, "Male");
enrolledStudents.add(Ryan);
student Rhys = new student("Rhys", "42 Newpark", 140306, "Male");
enrolledStudents.add(Ryan);
System.out.println(enrolledStudents.toString());
FileWriter writer = new FileWriter("StudentsDetails.txt");
for(student str: enrolledStudents) {
writer.write(str + System.lineSeparator());
}
writer.close();
}
I have tried adding toString() at the System out to see if it would convert but it doesn't but it still prints jibberish
EDIT:
I have moved it into the student class but now it won't write to a file
class student {
private String name;
private String address;
private int dob;
private String gender;
public student(String name, String address, int dob, String gender) throws IOException { // setting Student list order
this.name = name;
this.address = address;
this.dob = dob;
this.gender = gender;
ArrayList<student> enrolledStudents = new ArrayList<student>();
student James = new student("James", "23 Old Drive", 220298, "Male");
enrolledStudents.add(James);
student Matt = new student("Matt", "13 Wee Park", 110502, "Male");
enrolledStudents.add(Matt);
student Gary = new student("Gary", "1 Green Close", 240897, "Male");
enrolledStudents.add(Gary);
student Charlotte = new student("Charlotte", "5 Park Link", 230402, "Female");
enrolledStudents.add(Charlotte);
student Naomi = new student("Naomi", "133 Caulside", 180801, "Female");
enrolledStudents.add(Naomi);
student Ryan = new student("Ryan", "88 Wood Burn", 100602, "Male");
enrolledStudents.add(Ryan);
student Rhys = new student("Rhys", "42 Newpark", 140306, "Male");
enrolledStudents.add(Ryan);
FileWriter writer = new FileWriter("StudentsDetails.txt");
for(student str: enrolledStudents) {
writer.write(str.toString() + System.lineSeparator());
}
writer.close();
}
}