This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String ".equals" method always returning true?" Watch "String ".equals" method always returning true?" New topic
Author

String ".equals" method always returning true?

Billy Nicholson
Greenhorn

Joined: Jun 16, 2011
Posts: 8
Don't know where to go from here. Anything I could check?



I press any button which triggers an ActionEvent and I get:

Show/Hide Slave
Show/Hide Slave equals Show/Hide Slave
Show/Hide Slave equals Map

as the output.

Edit: Not only is it always returning true, it is also ignoring if/else flow.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
remove the semicolon at the end of line 11
Billy Nicholson
Greenhorn

Joined: Jun 16, 2011
Posts: 8
.....*facepalm* Woooow... I feel like an idiot. Thanks a bunch.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
Billy Nicholson wrote:.....*facepalm* Woooow... I feel like an idiot. Thanks a bunch.


No reason to feel silly, I think this is actually a common mistake and easy to overlook
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14952
    
  26

Note that a semi-colon by itself is an empty statement. So line 11 of your code means "if command equals "Map", then do nothing" - and the block in lines 12 - 18 is completely separate from the if-statement and is always executed.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
Have you checked out Aspose?
 
subject: String ".equals" method always returning true?
 
It's not a secret anymore!