-2

The input is a word. If the word is hello it should print: Hello sir etc if the input is not hello it should print: why dont you say hello? The problem is that it always prints: Why dont you say hello? Why is this?

Scanner input =new Scanner(System.in);
String word=input.nextLine();
if(word=="hello"){
    System.out.println("hello sir have a good day");
}
else{
    System.out.println("Why dont you say hello?");
}
mr_ecko
  • 61
  • 1
  • 1
  • 6
  • Have you considered option that it may not be scanner that is giving wrong result? Maybe it is way you are testing it? – Pshemo Jul 22 '15 at 12:49
  • depperm: that 's not the problem in this code, he's using == while he should be using equals – Stultuske Jul 22 '15 at 12:49
  • Changing nextLine to next gives same problem. How should i test it? what do you mean? – mr_ecko Jul 22 '15 at 12:50
  • Instead of `if(word=="hello")`, use `if(word.equalsIgnoreCase(hello))` – deezy Jul 22 '15 at 12:52
  • can you add `System.out.println(word);` at the end? Maybe the scanner gets some line-breaker symbols... – JohnnyAW Jul 22 '15 at 12:52
  • 1
    @JohnnyAW: nope. he's doing a referential comparison, while he wants to do a comparison on value. – Stultuske Jul 22 '15 at 12:53
  • Take a look at [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Pshemo Jul 22 '15 at 12:54
  • @Stultuske it doesn't matter in this case, since the `String`s are saved in the String-pool and no new Objects should be created here – JohnnyAW Jul 22 '15 at 12:55
  • This is not a real duplicate question. This is related to the internal implementation of Scanner that creates a new String using the operator new. So the returned string is not the same of internal string pool – Davide Lorenzo MARINO Jul 22 '15 at 12:57
  • 1
    @JohnnyAW: yes, it does matter in this case, and yes, it will solve his problem. If you don't believe me, just run the snippet. – Stultuske Jul 22 '15 at 12:59

1 Answers1

2

You have to use the method equals() instead of comparing the two strings with the operator ==

The == operator check for identity of the two operands.

In this case a new String is created when you call the method nextLine on Scanner. So calling the operator == results in a false results, instead calling equals that check for internal content of both strings returns true.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • 1
    Instead of reposting same answer all over again you can simply post comment with link to canonical question: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Bookmark it in your browser, you will need it. – Pshemo Jul 22 '15 at 12:53
  • Because here I explained why the results are different. Internally Scanner.nextLine calls inderectly the substring of String that create a new String. That's why I answered here. It is specifically to scanner this response. In addition is not a wrong answer so I don't understand the minus. – Davide Lorenzo MARINO Jul 22 '15 at 12:55
  • Thx to all i was very stuck. So the problem is the location where it is stored. == checks out the location – mr_ecko Jul 22 '15 at 13:02
  • Yes == check for location, equals check for content – Davide Lorenzo MARINO Jul 22 '15 at 13:03
  • "Because here I explained why the results are different." you add that part after my comment. Now your answer looks better. My comment was indented to discourage answers which basically say only *use euqlas instead of `==`*. – Pshemo Jul 22 '15 at 13:04