• Post Reply Bookmark Topic Watch Topic
  • New Topic

Captured String using Scanner not workin in if-statement  RSS feed

 
Andreas Bgr
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm new to Java and learning the basics at the moment

Can anyone tell me why when I write: "Monday" in the prompt I get "It does not work "?

when I use System.out.println(userInput) I get "Monday" but I cannot get it with the if-statement below... I must have done something wrong with the if-statement?

//k

 
Andreas Bgr
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm.. I get it to work using a switch-case statement = something is certainly amiss with that if... Can anyone see what it is?

This works:

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use equality operator with reference variables.
Use equals() method instead.

More information
 
Andreas Bgr
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha - thanks a bunch!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
The == operator← same link as you were given yesterday sometimes appears to work on Strings, but you should still avoid it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!