-1

i am coding a double linked list, in each node there are two slots, one for information and the other for data. Both inputs are choosen by user in console.The information slot is basically a string , meanwhile the Data slot must be an integer ( [information, data] <-- node is made like this,) now if i have more than one nodes on the linked list , i have to check if the new Node i am inputting into the list has the same Information and same data ( its ok if two nodes have same data OR same information)

    public void nodiInformazioneguale(String information, int key) {


    Node n = new Node(information, key, null, null);


    if (n.getInformazione() == head.getNext().getInformazione()&&n.getData( == head.getNext().getData()) {

        System.out.println("Insert another information && key");

    }

}
Artyy
  • 9
  • 4
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. In other words: what is your question? – GhostCat Jun 29 '17 at 12:07
  • Ah, too bad. You compare Strings, Integers, ... all Objects using **equals()**; not ==. Someone please close as dup. – GhostCat Jun 29 '17 at 12:07
  • You want to read https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – GhostCat Jun 29 '17 at 12:08

1 Answers1

0

You can insert your Node object in a Set, so if you override the equals/hashcode methods in Node class (using both data and information) you can have the check you insert a new Node, because you cannot have a duplicated object in a Set.

  • ty this should work – Artyy Jun 29 '17 at 12:13
  • But please note: this is probably "cheating" on your homework. You might want to ask your instructors if you are allowed to such other classes. You want to avoid that your input gets later rejected because of such things. Probably the idea is that **you** learn how to write code that makes sure that nodes are unique! – GhostCat Jun 29 '17 at 13:05