• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Could anyone point out why the do while will not terminate

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).

 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic