0

I want to search in linked list with scanner but i cannot do it. I can search without scanner. What is wrong with this code?

My search method:

    public void Search(Object data){
    Node tmp = head ;
    while(tmp != null ){

        if(tmp.getData() == data){
            System.out.println("Your input is in the list");
        }

            tmp = tmp.getNext();


    }

}

And my main class:

  public static void main(String[] args) throws ParseException {
    LinkedList list =new Linkedlist();
    ...... // adding methods etc.
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your input: ");
    String x=input.next();
    list.Search(x);
}
mancini13
  • 35
  • 1
  • 10
  • can you describe the error? – sihao Apr 12 '16 at 23:38
  • I write input as data which is in the list but it didn't match. Code doesn't give me an error but it does't work correctly. – mancini13 Apr 12 '16 at 23:42
  • 1
    @mancini13 "it doesn't work" is completely useless as a description for what's wrong. What doesn't work? How does it not work? "it didn't match" is closer, but you haven't included what you got, nor what you expected to get. – azurefrog Apr 12 '16 at 23:50
  • I expect that my input match with node's data. I cannot expect anything else from search method. – mancini13 Apr 12 '16 at 23:58

2 Answers2

1

You are passing the String parameter into search(data), which is being cast as an Object.

You will need to either type-cast the Object data parameter to type String, or change the method signature to explicitly accept type String

Adam S
  • 76
  • 1
  • 6
  • Yes, i tried all this things but anything did't change. – mancini13 Apr 13 '16 at 09:58
  • I would recommend running in a debugger with breakpoints to see exactly what is happening. I.e. exception, false negative find, etc. Without that, it will be difficult for anyone to assist further – Adam S Apr 13 '16 at 11:45
  • I did it too. Codes doesn't enter in if condition with scanner but when i use something like list.Search("input") it work correctly. – mancini13 Apr 13 '16 at 12:15
  • In that case, I would recommend editting your original question with a more complete versrion of code so others can build and help you debug – Adam S Apr 13 '16 at 12:20
  • I solved my problem. I implement head node as a null in constructor that is why it give me an error with equal method. – mancini13 Apr 13 '16 at 12:33
1

To build on what Adam has suggested, you can consider the following code:

public void Search(String data){

     Node tmp = head ;
     while(tmp != null ){

         if(tmp.getData().equalsIgnoreCase(data)){
             System.out.println("Your input is in the list");
         }

            tmp = tmp.getNext();
     }   
}

You should use .equals() or .equalsIgnoreCase() to compare the string instead of comparing by == which compares each other as objects.

What is the difference between == vs equals() in Java?

Community
  • 1
  • 1
sihao
  • 431
  • 2
  • 11
  • can you post the logs in your question? i guess, nullpointer might be a different problem – sihao Apr 13 '16 at 10:05