• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Evaluating Conditions for While loop

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this code, I can get out of the loop if the input is A, B, or C. I can't get out if it's "quit". How should I be evaluating the "quit" condition to get it to act like A, B, and C do?
while (principalCode.charAt(0) != 'A' &&
principalCode.charAt(0) != 'B' &&
principalCode.charAt(0) != 'C' &&
principalCode != "quit") {
principalCode = JOptionPane.showInputDialog
("Please enter A) for $1,000 B) for $5,000 C) for $10,000 or quit) for quit");
quit = principalCode.equalsIgnoreCase("quit");
}
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code:
principalCode != "quit"
is checking if the reference pointer �principalCode� is pointing to the String Literal you entered; it�s not. It is pointing to a String object. To see if that String object contains a string equal to "quit", you need to do this:
principalCode.equals("quit")
so to check for it not equaling "quit" you would do this:
!principalCode.equals("quit")
If you want to take case sensitivity out of the mix, so the input can be "Quit", "QUIT", "quit" or even "QuiT", you can do this:
!principalCode.equalsIgnoreCase ("quit")
Hope that helps
[ March 01, 2004: Message edited by: Mark Vedder ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition, I�m not sure what you are meaning to do with that last line of the where clause:
quit = principalCode.equalsIgnoreCase("quit");
The code you posted is not showing what quit is declared as, but I would have to assume it is a boolean since that is what String.equalsIgnoreCase returns. So that line is not really needed if you have this:

As an alternative, you could do this:

So I think you may have just been intermixing two alternative ways of accomplishing your goal.
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also find it better to use a do...while loop instead of a while loop, so that the test is at the end and you don't have to worry about initial values for principalCode and quit.

Also, note:
  • If the user enters a blank string, the call to charAt(0) will throw a StringIndexOutOfBoundsException.
  • If the user enters 'a' it will not be accepted as 'A'. Why are some inputs case-insensitive yet others are case-sensitive?
  • If the user enters 'ABORT' it will be accepted as 'A' because of the way you are comparing only the first character.


  • Why not use the String equals() method for comparisons instead, and rather than using lots of equalsIgnoreCase() calls just convert the input string into uppercase before comparing...
     
    Ruth Stout was famous for gardening naked. Just like this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic