(Java) I cannot loop over my arraylist object?

Viewed 79

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;
    }
}
2 Answers

in your for loop you are printing the detail object.

for(Item detail : itemList) {
    System.out.println(detail);
}

// prints
com.company.Item@69d9c55

first part is the package name (think folder sort of) and the random number after the @ is an hashed value of the object. So basically a sort of short textual representation of the class.

there is two ways of fixing this:

adding getters to the object to fetch the values you want to print:

public class Item {
    String name;
    String education;

    public Item(String name, String education) {
        this.name = name;
        this.education = education;
    }

    public String getName() {
        return name;
    }

    public String getEducation() {
        return education;
    }
}

// Then in your loop fetch the values
for(Item detail : itemList) {
    System.out.println(detail.getName());
    System.out.println(detail.getEducation());
}

or the slightly more advanced version which is overriding the toString function in the object. Every object has a function called toString which prints per default the object hash. You can customize this by, naming a function toString and then annotation the function with @Override. Then in that function you can write a custom string that you want printed, when you try to print the object.

public class Item {
    String name;
    String education;

    public Item(String name, String education) {
        this.name = name;
        this.education = education;
    }

    @Override
    public String toString() {
        return name + ", " + education;
    }
}

in your for-loop while iterating over your Item list (maybe you should redefine your ArrayList using polymorphism) you will be getting each time an object item and not a detail, so choose the name of your execution variable right.

From this object item you should should whether call item.getName() or / and item.getEducation() if you haven't implemented or overriden a public String toString() method for your Item class or @Override public String toString() from the automatic inherited java.lang.Object class.

With that been said, let us fix and optimize your code:


Main.java

package com.company;

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
    // write your code here

        Scanner scan = new Scanner(System.in);
        // Relying on the diamond operator
        List<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 item : itemList) {
            // Just call toString-method of Item class
            System.out.println(item.toString());
        }
    }
}

Item.java

package com.company;

public class Item {
    String name;
    String education;

    public Item(String name, String education) {
        this.name = name;
        this.education = education;
    }
    
    public String getName() {
        return this.name;
    }
    
    public String getEducation() {
        return this.education;
    }
    // you could also add some setters for these attributes if you need some later on
    
    @Override
    public String toString() {
        return "[Name: " + this.name + ", Education: " + this.education + "]";
    }
}

Hopefully, this will help you out. Also make sure you upvote the answer if it brings more details to you!

Related