This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Evaluating Conditions for While loop  RSS feed

 
Aaron Parker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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");
}
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • 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...
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!