• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with if statement  RSS feed

 
M Mangion
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am just a beginner in Java and I have a small problem with the following code. Whatever I enter into the firstname variable I always get the "else" statement to run. Can someone tell me what I am doing wrong please?

Thank you very much in advance.


 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Welcome to The Ranch!

You shouldn't use == to compare Strings. You need to use the equals() method. == checks if they are the same object, but you want to know whether they have the same value.
 
Dan Din
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

To check if two String (Objects) are equals, you must use the equals method, not "==" (which only works for primitives types).
So, in your exemple :



Good luck,
D.
 
M Mangion
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for your replies, it makes more sense now.

very much appreciated
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!