aspose file tools*
The moose likes Beginning Java and the fly likes Could anyone point out why the do while will not terminate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Could anyone point out why the do while will not terminate " Watch "Could anyone point out why the do while will not terminate " New topic
Author

Could anyone point out why the do while will not terminate

mick lynch
Greenhorn

Joined: Aug 17, 2008
Posts: 23
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

Joined: Jan 28, 2003
Posts: 3932
    
  17

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).


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Aug 17, 2008
Posts: 23
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could anyone point out why the do while will not terminate
 
Similar Threads
Can not find my error ? Help
DataOutputStream object might not have been initialized????
class As A Data Type
Adding strings together
Reading the first word of each line from a text file and then adding them to a Combo box