I have a code that reads a txt file It reads different Stude

I have a code that reads a .txt file. It reads different Student class and Course class attributes and pass them to an array of Student and an array of Course objects. The error I encounter is when I print the array of Courses, it prints the address of the array not the actually object. How can I correct my code so that I get the actual Course objects in the array, not the addresses? Please see my code from pastebin or check the screenshots below. I also commented where I\'m having problems.

Student Class: http://pastebin.com/G09517Sy, Driver Class: http://pastebin.com/F4A76h6z

while ((inputLine != null) && ! (inputLine \" equals (nines))) { ourse c new Course(); String[] ar= inputline . split (\",\"); //String course (ar [0]); //String credits = (ar [1]); //String grade = (ar [2]); c.setCourse (ar[0]) c.setCredits (ar [1]); c.setCourseGrade (ar [2]) s.setCourseArray (c) //System.out.println (c.toString )): inputLine = br.readLine() ;

Solution

You are getting this error because you didn\'t implemented the toString() method in both the classes. When you didn\'t implement the toString() method of a class, and if you try to print the object, then its address only will be printed. If you implemented toString() method of a class, then when you print the object, then it will print the return String value of toString() method. Please find below the modified code.

Here you need to iterate over each Course object in courseArray and print it seperately.

Please use the below given toString implementation for Student class, and also implement toString() in Course class(as the Course class source is not given, I can\'t do it here):

I have a code that reads a .txt file. It reads different Student class and Course class attributes and pass them to an array of Student and an array of Course o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site