-2

I am currently struggling to fix the outcome of my code.

I am supposed to add a list from menu and then display the list. However, I cannot retrieve its content, rather I receive its memory value (I guess?).

Studentclass

    private int number;
    private String author;
    private String title;

    public Student() {
    }

    public Student(int number, String title, String author) {
        this.number = number;
        this.title = title;
        this.author = author;
    }

    public int getNumber() {
        return number;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String ToString() {
        return "Number: " + number + "\tTitle: " + title + "\tAuthor: " + author;
    }

Mainclass

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        ArrayList<Student> newStudents = new ArrayList<Student>();

        System.out.println("Please select a number from the options below \n");

        while (true) {
            // Give the user a list of their options
            System.out.println("1: Add a student to the list.");
            System.out.println("2: Remove a student from the list.");
            System.out.println("3: Display all students in the list.");


            // Get the user input

            int userChoice = input.nextInt();
            switch (userChoice) {
                case 1:
                    addStudents(newStudents);
                    break;
                case 2:
                    //removeStudent(newStudents);
                    break;
                case 3:
                    displayStudent(newStudents);
                    break;
            }
        }
    }

    public static void addStudents(ArrayList<Student> newStudents) {


        Scanner input = new Scanner(System.in);

        Student newStudent = new Student();

        System.out.print("Please enter number: ");
        newStudent.setNumber(input.nextInt());

        System.out.print("Please enter title: ");
        newStudent.setTitle(input.next());

        System.out.print("Please enter author: ");
        newStudent.setAuthor(input.next());

        if (newStudents.size() <= 100) {
            newStudents.add(newStudent);

            System.out.println("Student added\n");
        } else {
            System.out.println("\n Student interface is full!");
        }

    }

}

    private static void displayStudent(ArrayList<Student> newStudents) {


        for (Student e : newStudents) {
            System.out.println(e);
        }
    }
}

Output:

1: Add a student to the list.

2: Remove a student from the list.

3: Display all students in the list.

3

Student@6b2acb7a

Why @6b2babc7a?

Thank you for your kind help and attention. I'm roughly new to programming, and Java is my first language. So, I'd highly appreciate the help and clarification.

bwnboii
  • 5
  • 6

2 Answers2

0

You have to override public String toString() in Student class to provide String when used in System.out.println()

But you have public String ToString() change it to public String toString().

With out toString() method in the Student the toString() method from java.lang.Object will be called which will return the hashcode of the instance.

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
0

When you call print any object in Java, internally toString() method of the class is called. As in Java Object class is parent of all classes and toString() method is available in Object class. So this method is available to all Class objects.

By default toString() on an object return getClass().getName() + '@' + Integer.toHexString(hashCode()).

So as a result you are getting Student@6b2acb7a as the output. If you want to print something else, you need to override the toString() in your Student class and whatever you return from that method, will get print.

Method in Object class is named as toString(). So you need to do like:

@Override
public String toString() {
        return "Number: " + number + "\tTitle: " + title + "\tAuthor: " + author;
    }

IMPORTANT POINT : When you override any method from super class, annotate it with @Override annotation. If you are Overriding it incorrectly, you would get the compilation error. And it's always better to find the issues at compile time, rather than runtime. If you would have done that, you would have already find the issue.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47