-1

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();


                
        }
    }
  • 2
    _Where_ did you add `toString`? What, exactly, did you write in `toString()`? Writing a correct, useful `toString()` method is the right solution, almost certainly. – Louis Wasserman Apr 15 '21 at 17:35
  • FileWriter writer = new FileWriter("StudentsDetails.txt"); for(student str: enrolledStudents) { writer.write(str.toString() + System.lineSeparator()); } writer.close(); This is where I assumed it would work. – Matthew Finlay Apr 15 '21 at 17:39
  • 1
    toString() is what is used by default, you have to implement toString in student (should be Student btw.) – Turo Apr 15 '21 at 17:39
  • 1
    You need to have a custom `toString()` method inside your `student` (why not `Student` ?) class. – PM 77-1 Apr 15 '21 at 17:40
  • 2
    @MatthewFinlay You haven't written a `toString()` method, all you've done is *call* the *default* `toString()` method. And you havent even really done that, because it was already being called implicitly. You have to actually *write* the `toString()` method ***in the student class***. – Charlie Armstrong Apr 15 '21 at 17:41
  • 1
    Updated it with an Edit :) – Matthew Finlay Apr 15 '21 at 17:57
  • @MatthewFinlay: Nope, that's not what you need to do either. You need to write something like `public class Student { ... @Override public String toString() { /* create a string representing this student. If you use the word FileWriter in this function, you're doing it wrong. */ } }` – Louis Wasserman Apr 15 '21 at 18:55
  • @LouisWasserman so the reason it doesn't work is because i'm setting the list order of the class student? – Matthew Finlay Apr 15 '21 at 19:46
  • @MatthewFinlay: Nope. Nothing to do with lists. The problem is that `Student` needs to override the `toString()` method. – Louis Wasserman Apr 15 '21 at 20:58

1 Answers1

0

within your student class, you have to override the toString method. Like this:

@Override
public String toString() {
    // here you return what you want to see. In this case I'll print its name
    return name;
}

Also:

  • it is important to rename your class student to Student. By convention classes names must be in CamelCase.
  • Don't put logic into the constructor. Your first piece of code is better than your edited code.
elbraulio
  • 994
  • 6
  • 15