0

I created a program with a new class called Student that sets and gets different variables such as name, GPA, if they are enrolled and when they are enrolled (this is a String, it is intended to be). The problem is that when I try to store the information into an array and/or array list and print it I get the hash code printed instead of the information. The program is not entirely complete, but I am trying to address the hash code issue first. If you could assist I would much appreciate. Thank you in advance!

    Student st = new Student();


    System.out.println(st.getName());
    Student1();
    Student st1 = new Student("John");

}

public static void Student1() {

    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();
    student[i] = newStudent;

    strList.add(newStudent);
    System.out.println(student[i]);

}

Student Class

public class Student {

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

public Student() {

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

}

public Student(String name1) {
    this.name = name1;
    this.gpa = 0.0;
    this.enrolled = false;
    this.enrollDate = "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) {
}

}

user2946846
  • 27
  • 2
  • 6

4 Answers4

2

This is because you are putting a student object into the array:

Student student[] = new Student[5];
newStudent.getName();
student[i] = newStudent;

strList.add(newStudent);
System.out.println(student[i]);

Here when you are printing, you are not printing the students name, but rather the object itself. Whenever you do a System.out.println on an object the hashcode is returned.

Was this your problem?

Arash Saidi
  • 2,228
  • 20
  • 36
1
  1. If you want a String representation of an object, this object must implement toString(). If you don't the default in the JVM is to print the hash code.
  2. If you want to print a specific field of your object, you should explicitly print that field, through a method if necessary; like System.out.println(student[i].getName());
  3. If you are trying to print a whole array, you should use Arrays.toString().
Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • I apologize for the MASSIVE delay, I got called into work and got home after midnight. BUT your suggestion fixed it, thank you sir or madam. You saved me quite a bit of time. – user2946846 Nov 08 '13 at 15:12
  • Don't worry, I had answers accepted months later :) – Cyrille Ka Nov 08 '13 at 19:55
0

please override toString() method as currently it is only printing by default implementation of toString() method from Object class which is set to current object's hashcode().

jasan
  • 21
  • 2
-1

This kind of printing will get the instance address and not a print of it.

Because you want to print a reference type, the VM doesn't know how you want it display or whatever or not some of the fields aren't for printing.

It's apt to you, Overriding "public String toString()" method and implement your own format.

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36