Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could anyone point out why the do while will not terminate

 
mick lynch
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone point out why the do while will not terminate when x is entered, it just continues to ask for
the stringgs to be entered without stopping as I thought it should when an x was enterd ?


[edit]Add code tags. CR[/edit]
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are Objects, and so when you compare them with the == operator you are testing to see if "x" is the same String Object as firstString, which it apparently is not. If you want to test if the content of firstString is the same as "x" (whether it is the same actual object or not) you should use the .equals method, like you should when comparing all objects.


When comparing Strings to a String constant I always put the constant first to prevent null pointer exceptions (or the need to test for null first).

 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The "==" checks to see if the references are the same -- ie. the same object. It doesn't check for the equality of the string values. And in your example, these "if" statements will never succeed.

[Hmmm.... beaten to the answer again... ]

Henry
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please us the code tags; I have added them so you can see how much better your post looks.

Never write == false or similar; you can get all sorts of nasty errors if you mistakenly write = for ==. If you want something to be false you write while (!flag) and to get it true you write while (flag). So you can simply write while (flag).

Lots of beginners get confused about == when using Strings. The == only tests whether the two Strings are the same object in memory; you need to use the equals() method. The String entered from the JOptionPane is not going to be the same String as "x", not even if the two have identical content. You can read about equals here, and here and here.

You would do well to look through the String class for methods which start with equals; there is another rather similar method which might be useful.
 
mick lynch
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I had tried the equalsIgnoreCase(), it ran a little differently but i will have to rethink all the code as its just not happening the way I want. Thanks for putting me on the path!
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic