0

I am making a program that creates a new class called Student that sets and gets a few different variables. I have everything essentially working except when I try to store the array elements that print out properly into my ArrayList, I get hash code instead. I'm sure altering one or two minor things will fix it, but I am at my wits end. Any and all suggestions appreciated, thank you!

Main:

    Student st = new Student();
Student();
}

public static void Student() {

    System.out.println("Enter the data for student 1 :");
    Student st = new Student();
    Scanner keyboard = new Scanner(System.in);

    String name = keyboard.next();
    double GPA = keyboard.nextDouble();
    boolean enrolled = keyboard.nextBoolean();
    String date = keyboard.next();
 for (int i = 0; i<5; i++){
    Student newStudent = new Student();
    newStudent.setName(name);
    newStudent.setGPA(GPA);
    newStudent.setEnrolled(enrolled);
    newStudent.setDate(date);
    ArrayList <Student> strList = new ArrayList<Student>();
    Student student[] = new Student[5];
    newStudent.getName();
    newStudent.getGPA();
    newStudent.getEnrolled();
    newStudent.getDate();
    newStudent.toString();
    student[i] = newStudent;
    ArrayList myList = new ArrayList();
    myList.add(student[i]);
    System.out.println(student[i].getName());
        System.out.print(student[i].getGPA());
        System.out.print(student[i].getEnrolled());
        System.out.print(student[i].getDate());
     System.out.println(myList);
 }

 }

Student Class :

public class Student {

private String name;
private double gpa;
private boolean enrolled;
private String date;

public Student() {

    this.name = "";
    this.gpa = 0.0;
    this.enrolled = false;
    this.date = "none";

}

public Student(String name1) {
    this.name = name1;
    this.gpa = 0.0;
    this.enrolled = false;
    this.date = "none";
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return this.name;
}

public void setGPA(double gpa) {
    this.gpa = gpa;
}

public double getGPA() {
    return this.gpa;
}

public void setEnrolled(boolean enrolled) {
    this.enrolled = enrolled;


}

public boolean getEnrolled() {
    return this.enrolled;
}

public void setDate(String date) {
    this.date = date;
}

public String getDate(){
return this.date;
}
 }
user2946846
  • 27
  • 1
  • 6
  • 1
    try System.out.println(myList.toString()); and override the toString() method in your Student class. – wxyz Nov 08 '13 at 16:57
  • Just tried it, same result unfortunately. – user2946846 Nov 08 '13 at 16:58
  • You are creating a new myList every iteration - is this what you intended? Maybe you intended to create one list and add each student to it? – pillingworth Nov 08 '13 at 17:00
  • also override the toString() method in your Student class – wxyz Nov 08 '13 at 17:01
  • Pauli, I am to print the results in an array and arraylist for 5 students, so I think I would not want to create a new myList every time. Should removing it from the loop fix that? wxyz, how do I got about overriding toString? – user2946846 Nov 08 '13 at 17:05

2 Answers2

3

Arrays by itself won't print their contents using toString method. Instead, use Arrays.toString(yourArrayVar) or Arrays.deepToString(yourArrayVar).

Also, don't forget to override the toString method in your Student class as well, otherwise it will use Object#toString, that ends printing the data in this format (from JavaDoc):

getClass().getName() + '@' + Integer.toHexString(hashCode())

Luiggi Mendoza
  • 83,472
  • 15
  • 149
  • 315
0

When you attempt to print the ArrayList, with this line,

System.out.println(myList);

toString() is called. It attempts to convert each of its items into a String by (eventually) calling toString() on your Student. You don't override toString(), so the "hash code" you see is the default toString() from Object.

Override toString() to convert your Student to a String as appropriate.

@Override
public String toString()
{
    // Construct a String here that is appropriate for this class.
    // Return it!
}
rgettman
  • 172,063
  • 28
  • 262
  • 343
  • That makes sense, how am I able to override toString? – user2946846 Nov 08 '13 at 17:08
  • I think I understand your suggestions, would I override for each method that gets the variable? ie (if readable with the comment format) public String getName() { @override public String toString() return this.name;} – user2946846 Nov 08 '13 at 17:15
  • No, `toString()` is just one more method to include in the `Student` class. It's not something to include in each method. (You can't nest methods like that anyway.) – rgettman Nov 08 '13 at 17:20
  • Okay, I believe understand what you're saying but I am not sure how to do it. Within that method what am I going to be doing? Overriding the prewritten toString method I understand, but with what? This is totally foreign to me, I apologize. – user2946846 Nov 08 '13 at 17:27