Skip to content
Advertisement

Java: How to use a Scanner to check that input is an integer and is within a specified range

I’m having trouble with my code.

What it should do:

  • Check if Scanner “myScanner” is an Integer
  • If it is an Integer, if it is between 1 and 200 (both included)

Problem:

  • I need to input an Integer with the correct value twice after the the first guess wasn’t correct.

Here is a screenshot of exactly what I mean: Screenshot of the problem

JavaScript

I have tried to apply @Hulk’s (top answer) logic (at least I think I did) into a single method (is sadly a requirement for my project) and got this:

JavaScript

After a bit of testing still no error! If you still find a mistake I would be happy if you shared it with me!

Advertisement

Answer

This gets a lot simpler if you split your problem into smaller parts. First, solve the “read until we got an integer” part and put it into a method so we can reuse it:

JavaScript

Now, we only need to add the range check:

JavaScript

You basically complicated things too much, by repeating the range check in 3 places in different ways, and it’s easy to get lost with all these negations. Keep your methods simple, and only do one thing at a time!


To adress your updated question: If you absolutely need to inline these into one method, this is the result:

JavaScript

Note that there is still only one place where there is a check for numeric input, and only one place where the range is validated. In programming, there is rarely a reason to write exactly the same thing twice – don’t repeat yourself! – a ‘principle’/guideline sometimes abbreviated as DRY.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement