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.