• Post Reply Bookmark Topic Watch Topic
  • New Topic

Quit, Error, or Enter  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a problem where I take a users input and one of three things can happen based on what the user does.

The question asks the user to Press 'enter' to continue, or "Q" or "q" to quit and end the program. If they enter something other than "Q" or "q" though there is an error message and the program loops back up to ask the initial opening question again.

The problem is when the user only hits enter it evaluates within the "else if" part of the program....I don't want it to read when the user hits the 'enter' key alone as an error, but to continue with the program.

Here's what I've been working with:

 
Saloon Keeper
Posts: 3331
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like you need to test for three states: q/Q, or Enter, or else it's an error. You can test for Enter with quit.length()==0. Note that with your current code your else-if could be replaced with just an if because you've already determined the q/Q state. With three states you'd need: IF, ELSE-IF, and ELSE.
 
Ursula Coyote
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you! I'm going to look into the quit.length()==0.
 
Ursula Coyote
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you! I was able to solve the issue.

I was thinking of the 'enter' key as something and trying to define it....as opposed to thinking of it as 'nothing'. I hope this make sense?
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope it isn't too late to welcome you to the Ranch

Beware of nextLine(). What does your book say it does? That is probably wrong. You can use nextLine with no problems if you have not used that Scanner for anything else beforehand. Using nextLine twice means the following input will be discarded. So what you write at the keyboard and what it received by the program will be out of phase with each other.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!