• Post Reply Bookmark Topic Watch Topic
  • New Topic

Or in while conditions not behaving the way I'd think  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just trying to let the user select from a menu of options 1-5, and thought I'd go the extra mile and let them try again if the input isn't one of those numbers.

I did this to check the input:



But it's not working. When I remove the || sections and just use !getinput.hasNextInt() in the while conditions, it correctly rejects words and non-ints, but allows entries lower than 1 and higher than 5. The while conditions read to me like "if the input isn't an integer, or the input is equal to/lower than 0, or the input is equal to/higher than 6, ask the user to input again" but that isn't how it's going. Help?

 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are reading your input twice in one condition! getinput.nextInt() <= 0 || getinput.nextInt() >= 6
Don't do that. Assign the input to a variable and then make a comparison.
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what the line of code is doing.
You are checking if there is not an input, if there is an input you are getting it and checking it is less and or equal to 0, if it's not you are getting the next input - you've already got the input are you sure you want to get the next one and will there even be a next one?
 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, I thought I could check the value of nextInt before placing it in a variable. I'll have to figure out a loop structure that first checks if the input is an int, then places it in a variable, then checks the value for validity, then repeats the whole process if it fails the validation?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Allen wrote:I see, I thought I could check the value of nextInt before placing it in a variable. I'll have to figure out a loop structure that first checks if the input is an int, then places it in a variable, then checks the value for validity, then repeats the whole process if it fails the validation?

The last line of your while loop gets the next int but doesn't assign it to anything. you should store the value in a variable that variable could be used in the while conditional clause to do the bounds test.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:The last line of your while loop gets the next int but doesn't assign it to anything. You should store the value in a variable that variable could be used in the while conditional clause to do the bounds test.

Isn't it just read for discarding a value that is not an int?
 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have it working now, it looks like this:



The only problem is when the second while rejects the value in choice due to being out of bounds, it doesn't prompt again. Well, it looks for more input and works properly, but it doesn't tell the user that their input was rejected. Should the second while be a second do-while instead? Is that even a real thing?

Thanks for the help so far, I definitely understand what I screwed up on the first time around.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't try random things and check whether they work or not!
Think before you code!
StopCoding <- click this
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Tony Docherty wrote:The last line of your while loop gets the next int but doesn't assign it to anything. You should store the value in a variable that variable could be used in the while conditional clause to do the bounds test.

Isn't it just read for discarding a value that is not an int?

I was about to say it really depends on what getinput is but Ben has just posted some more code that shows it is a Scanner so yes you are right.
 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Don't try random things and check whether they work or not!
Think before you code!
StopCoding <- click this


OK, OK. I bought some index cards and a clipboard, I'll try using them and come back to this later.
 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ended up discovering try/catch, and using that to validate that the input is an integer, clearing the input after a failure so the user can try again. Once I have an integer, bounds checking is done by a while loop. Is this sane?

The assignment only requires that I make sure entries aren't negative, but I wanted to go the extra mile and make sure they are also valid. My main method instantiates this class, and uses the returned number to do what the user selects.



I know using catch(Exception) is frowned upon, and I'm supposed to use more specific exceptions, but this is already going above and beyond the assignment, so I don't want to spend more time on it.

Please let me know if I've done this in a stupid way. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!