1

Hi i have the following code:

public List<Person> findAll() {

    List<Person> copy = new ArrayList<Person>();
    for (Person person : personer) {
        copy.add(person);
    }

    return copy;

}

But when i test this i only retrieve the following and not the value:

[Person@15c7850, Person@1ded0fd, Person@16a9d42]

How do i get the values and not like above. Where i am inserting the person the code looks like this:

public boolean insert(String name, String nbr) {

    if (containsName(name)) {
                    return false;
            }
    Person person = new Person(name, nbr);
    personer.add(person);

            return true;
}

and here is my Person class:

class Person {

private String name;
private String nbr;





public Person (String name, String nbr) {
    this.name = name;
    this.nbr = nbr;
}


public String getName() {
    return name;
}


public String getNumber() {
    return nbr;
}
}
2
  • 2
    What do you mean "values"? Is there some field on the person class that you are expecting to see? Commented Dec 25, 2010 at 14:42
  • i have in the list saved the name and the telephone number of the person... i want to show that Commented Dec 25, 2010 at 14:44

6 Answers 6

4

You're already receiving the objects you want.

What you see is an internal representation of these objects.

You must iterate through them and call their respective methods to see the information you probably want to see.

If you're not satisfied with these results, you must override toString to provide you with more meaningful information.

Update:

after seeing your edit, you should add toString similar to this one in your Person class:

@Override
public String toString() {
    return "Name: " + name + ", number: " + nbr; 
}

By the way, you're storing nbr as a string, and it's obvious it should be an integer. So, I'd suggest changing its type to an int or Integer.

Sign up to request clarification or add additional context in comments.

5 Comments

@Jake: toString() will show you a "default" value. Since Java doesn't know anything about context of your class, it will just print its internal representation of your class. As I said, you must override toString to show you what you really want. Although Java tries to be smart, it cannot always guess your intentions.
go to Person class and create a new toString method, there you can return for example: return "Name" + this.name; Do that to all attributes you got on your class in a single String to return meaningful information as @darioo said.
I added some more information in my question above. That is how i am inserting the person.
@Jake: your newly added code won't help us much. You should also show us your Person class.
This is only a good idea if you are looking to print something. If you actually want to use the values, you have to do something else in the context of what is going on here. It is pretty much useless if you want to do things like Person lookups via name/number.
2

You are getting a List object back. You can use the Person object to get the data that you need. To get to the Person objects, iterate over the list.

List<Person> people = findAll();
for Person p : people {
    String phoneNumber = p.phoneNumber();
    String name = p.Name();
}

Comments

1

Override the toString() method in the Person class if you want a better description when printing the results.

Comments

1

Put something like this in the class Person (don't change the method name!):

public String toString() {
    return name;//change this line
}

Comments

1

You are printing out an Object that has the default toString inherited from the Object class. This will print out the type of object it is and its location in memory (ie: Person@1ded0fd).

If you'd like it to see something else, you can override the toString method within your class:

public class Person {
  private String name;

  public Person(String name) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }    

  public String toString() {
    return this.name;
  }
}

If your class looked like the above, this would allow you to do something like this:

Person p = new Person("John");
System.out.println(p);
 > John

You can also just grab it as is and print out any information you want from it without overriding the toString method.

Person p = new Person("John");
System.out.println(p.getName());
 > John

Comments

0

What value or class Person's property you aspect to retrieve from the ArrayList? This kind of value(Person@15c7850, etc) shows that the Person's object random id that assigned by JVM when you use

System.out.print(copy).

1 Comment

It's not random; it's the address of the reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.