Neo
Neo

Reputation: 117

How to display contents of an object in Java

Ok, now I think I've given up all hope of finding solution to what should to be a simple problem. Basically, I'm creating a students' record system that stores students' details in an ArrayList. I first created a constructor in the Student class to specify what entries each student will have. I then created an instance of the Student class in the main class (i.e. class with the main method) and then added the student object to the studentList ArrayList.

By the way, instead of hard-coding the student details, my initial aim was to let the user enter the details and then I'll use a Scanner or BufferedReader object to get the details stored in the Student object, and then to the ArrayList but I'm having trouble with that as well; so I'll probably tackle that problem as soon as I'm done with this one.

Anyway, I'm expecting the output to print out the students' details but instead I get a memory location (i.e. [studentrecordsys.Student@15c7850]). I'm aware that I need to override the toString method but how exactly this is done is what I can't seem to get. I get syntax errors everywhere as soon as I enter the @Override code block for the toString method. Here's what I've tried:

import java.util.*;

class Student {
    private String studentID;
    private String studentName;
    private String studentAddress;
    private String studentMajor;
    private int studentAge;
    private double studentGPA;

     Student (String studentID, String studentName, String studentAddress, String 
             studentMajor, int studentAge, double studentGPA){

        this.studentID=studentID;
        this.studentName=studentName;
        this.studentAddress=studentAddress;
        this.studentMajor=studentMajor;
        this.studentAge=studentAge;
        this.studentGPA=studentGPA;
     }
}


public static void main(String[] args) {
Student ali = new Student("A0123", "Ali", "13 Bond Street", "BSc Software Engineering", 22, 3.79);
    List<Student> studentList = new ArrayList<>();

    studentList.add(ali);

    @Override
    String toString() {                        
        StringBuilder builder = new StringBuilder();
        builder.append(ali).append(studentList);
        return builder.toString();
    }

    System.out.println(builder);
}

Upvotes: 0

Views: 26539

Answers (3)

Kick Buttowski
Kick Buttowski

Reputation: 6739

You to override toString method because it is going to give you clear information about the object in readable format that you can understand.

The merit about overriding toString:

Help the programmer for logging and debugging of Java program

Since toString is defined in java.lang.Object and does not give valuable information, so it is good practice to override it for subclasses.

Source and Read more about overriding toString

public String toString() { 
    return "Studnet ID: " + this.studentID + ", Student Name:"
           + this.studentName+ ", Studnet Address: " + this.studentAddress
           + "Major" + this.studentMajor + "Age" + this.studentAge
           + GPA" + this.studentGPA ;
} 

Upvotes: 2

daentech
daentech

Reputation: 1115

You need to implement the toString() on the Student object.

public class Student {
    ...
    Your existing code
    ...

    @Override
    public String toString() {
        return studentID + ", " + studentName + ", " + //The remaining fields
    }
}

then in your main method, call

for (Student student : studentList) {
    System.out.println(student.toString());
}

Upvotes: 2

gkrls
gkrls

Reputation: 2664

You get errors because you have to Override the toString method inside the class you want to use it for. i.e you have to put the method, with the @Override inside your Student class.

And you can call it like this:

System.out.println(studentA.toString());
System.out.println(studentB.toString());

or in a loop:

for(Student x : studentList)
    System.out.println(x.toString());

and so on..

Also, in your code you create a method inside your main method. Of course you will get errors.

Upvotes: 1

Related Questions