This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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).
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.
Joined: Aug 17, 2008
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!
Joined: Oct 13, 2005
subject: Could anyone point out why the do while will not terminate