I'm trying to loop over an ArrayList object but I only get the following:
Name & Education: com.company.Item@69d9c55
I have no clue why this is happening. I'm using the code provided in the MOOC course that I 'm following online, but am trying to redo this in my own small test project as I want to put what I learned into practice.
Here's the code: Main
package com.company;
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner scan = new Scanner(System.in);
ArrayList<Item> itemList = new ArrayList<>();
Item item;
while(true) {
System.out.println("Name plz?");
String name = scan.nextLine();
if (name.isEmpty()) {
break;
}
System.out.println("Education plz?");
String education = scan.nextLine();
if (education.isEmpty()) {
break;
}
item = new Item(name, education);
if (!itemList.contains(item)) {
itemList.add(item);
}
}
System.out.println("Name & Education: ");
for(Item detail : itemList) {
System.out.println(detail);
}
}
}
and the main Class
package com.company;
public class Item {
String name;
String education;
public Item(String name, String education) {
this.name = name;
this.education = education;
}
}