-1

I am trying to create a linked list with Objects from my Person class which i have attached but i cannot seem to print the actual names i have created for each object. i can only print the typical .toString() output of "gibberish". I need a way to print out the names while still using the Objects.

public class personlink {

public static void main(String[] args)
{
    LinkedList<Person> list = new LinkedList<Person>();

    Person Alice = new Person("Alice");
    Person Brady = new Person("Brady");
    Person Cathy = new Person("Cathy");
    Person Danny = new Person("Danny");
    Person Amy = new Person("Amy");
    Person Eddie = new Person("Eddie");
    Person newp = new Person("New Person");

    list.add(Alice);
    list.add(Brady);
    list.add(Cathy);
    list.add(Danny);
    //
    System.out.println("Linked List Content: " + list);
    //
    list.addFirst(Amy);
    list.addLast(Eddie);
    //
    System.out.println("LinkedList Content after addition: " + list);
    //
    list.removeFirst();
    list.removeLast();
    //
    System.out.println("LinkedList after deletion of first and last element: " + list);
    //
    list.add(0, newp);
    list.remove(2);
    //
    System.out.println("Final Content: " + list); 

}
}


public class Person 
{

String name;


public Person(String _name)
    {
        name = _name;
    }

    public String getName()
    {
        return name;
    }
}

1 Answers1

0

Edit the Person class adding the toString() method.

freedev
  • 25,946
  • 8
  • 108
  • 125
  • The question's an obvious duplicate of many previous ones. – Hovercraft Full Of Eels Apr 05 '17 at 00:11
  • Right, so just to understand, what's the correct behaviour? Avoid to answer and mark it as duplicate? – freedev Apr 05 '17 at 00:15
  • It is not an obvious duplicate. I am trying to find the Object value and print it. No other question i have seen is doing that. If it is so obvious i would love your assistance instead of just marking it a duplicate – Joey Freitas Apr 05 '17 at 00:16
  • Yes, especially if it's such a common "core concept" error such as this one, and for example NullPointerException type of questions. – Hovercraft Full Of Eels Apr 05 '17 at 00:17
  • 1
    @JoeyFreitas: yeah, it is, like a million other such similar "my println statement is printing gibberish" type questions. It is a duplicate and for anyone semi-versed in Java, it is obvious and the question should be closed. – Hovercraft Full Of Eels Apr 05 '17 at 00:18
  • @HovercraftFullOfEels thanks for the explanation, it's the second time I received a comment like your. Do you know if there is a SO page that explain how to act in these cases? – freedev Apr 05 '17 at 00:18
  • @freedev: best to become familiar with the [SO meta site](https://meta.stackoverflow.com/) since these types of issues are frequently discussed there. – Hovercraft Full Of Eels Apr 05 '17 at 00:20
  • @JoeyFreitas: please have a look at [this similar question and its answers](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4?noredirect=1&lq=1) – Hovercraft Full Of Eels Apr 05 '17 at 00:20