This is not a very pleasant approach. You might get an InputMismatchException too often ( just an extra space before a 5 would give you an InputMismatchException).
If this is not what you want, you could try scanner.nextLine().trim() first.
This would format your input so leading and trailing spaces are removed. Once you have the formatted ( and hence slightly more safe ) input, you could use a parseInt. parseInt throws a NumberFormatException so you'd need to provide a NumberFormatException handler.
Coming to how do you get to ask the user to input the number again, you have to put that thing of taking the user input inside a loop so it repeats. while (true) is a good option.
Or while (!someBooleanFlag) is something you could try.
Please DontWriteLongLines. It makes your thread hard to read,
and it's actually bad coding practice.
I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.
I think in your case, it was mainly due to the fact that your indentation is screwed up.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
create, convert, edit or print DOC and DOCX in Java