Blue J, my listMembers method is not printing off the correct data from my Array List

70 Views Asked by At

When I add a member object from a class called Member to an array list "members = new ArrayList();" within a class called Library, and then try and print off a list of data within the array list I get an error message in the print out:

Member@12eb9b8
Member@172d568

The method I am using to add the member objects to the array list is:

public void joinMember(Member joinMember)
{
    members.add(joinMember);
}

And the method I am using to print out all of the data within the array list is:

public void listMembers()
{
    for(Member joinMember : members){
         System.out.println(joinMember);
    }
}

Can anyone please help? I have checked over the code and I cannot work out why it's not printing out the correct data.

3

There are 3 best solutions below

1
On BEST ANSWER

That is not an error, it seems to be the value of instantiated object references. If you want to print what is inside, you either need to access something from the object to print, like

object.getSomething(); // Assuming the getSomething() method prints something 
                       // useful from within the class

or

Add a toString() method to the class.

0
On

It seems to be printing the correct data. The "problem" is that you are printing only the "object address". You should add some method in your Member class to print some information that you wish and use it in your "Sysout" method:

System.out.println(joinMember.getName());

Let me know if that's what you want.

0
On

What you're seeing is the Object implementation of the toString method. It's not an error. If you want it to print out something meaningful, you can override the toString method in the Member class.

Example

public String toString() {
    // name is just an example. can be anything.
    return this.name;
}