0

Working on a school project here and a bit stumped. This is a beginner java course, so bear with me if my explanation is a bit crude.

So for this assignment I am given a string array of student data that I have to split to remove commas and recreate as an object of my Student class as an ArrayList. I am able to split, remove commas and pass the information back into an ArrayList I've called kids but when I try to iterate over the ArrayList called kids I am getting memory addresses instead of the actual Strings of "split" info.

My output is:

Student@74a14482

Student@1540e19d

Student@677327b6

Student@14ae5a5

Student@7f31245a

The project is incomplete at this stage, there are some methods I need to implement but I can't finish up until I figure out how to get this to print properly.

My research has told me that maybe I need to use the .toString method, but not sure where...

Roster.java

public class Roster {

static String[] students = {"1,John,Smith,John1989@gmail.com,20,88,79,59",
            "2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
            "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87",
            "4,Erin,Black,Erin.black@comcast.net,22,91,98,82",
            "5,David,Reeves,deemreeves@gmail.com,33,81,95,89"};

/* Create ArrayList */
static ArrayList<Student> kids = new ArrayList<Student>();

public static void main(String[] args) {
        new Roster();
    }


public Roster(){
    for (int i = 0; i < students.length; i++) {
        String s = students[i];

        /* split Students array and remove commas and place strings into array named parts */
        String[] parts = s.split(",");

        /* assign values from split students array to variables to be passed into a Student Object */
        String StudentID = parts[0];
        String firstname = parts[1];
        String lastname = parts[2];
        String email = parts[3];


        Student a = new Student(StudentID, firstname, lastname, email);
        kids.add(a);

    }

    System.out.println("Why is this printing out as a memory address instead of the contents of the array list!");
    for (Student a : kids){
        System.out.println(Arrays.toString(a));
    }
 }

Student.Java

public class Student {

/* Instance Variables */

String studentID;
String firstname;
String lastname;
String email;
int age;
int grades[];

public Student(String studentID, String firstname, String lastname, String email) {


    this.studentID = studentID;
    this.firstname = firstname;
    this.lastname = lastname;
    this.email = email;

}

}
  • 3
    You need to define/override a `toString` method on your `Student` class, see for example http://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java – Mark Rotteveel Apr 13 '16 at 16:06
  • That's what I thought but I'm not sure how to add then within my student class. Do I have to add an @override for each variable in the Student class? I am really new so I'm a bit confused here :/ – David R Apr 13 '16 at 16:12
  • No you need to add an implementation of `toString()` that returns the string you want; as explained in the linked duplicate. – Mark Rotteveel Apr 13 '16 at 16:15
  • I had read that post but I finally got it, thanks for encouraging me to keep playing with the solution there. I added @Override public String toString() { return studentID + firstname + lastname + email; } – David R Apr 13 '16 at 16:18

0 Answers0