As @Elliott Frisch's answer states, the .nextInt()
call is just not going to return until an actual number of some sort is entered (or, if something else is submitted by the user, the InputMismatchException
occurs instead.
One easy alternative is to just.. not call .nextInt()
then. call .next()
, check if the resulting String is empty, and if not, turn it into an integer using: int userNumber = Integer.parseInt(theStringYouGotFromScannerNext);
.
NB1: Java convention states a variable is named 'userNumber1', not 'user_number1'. When in Rome and all that.
NB2: If you want your scanner to read 1 answer every time the user presses enter, call scanner.useDelimiter("\r?\n");
immediately after new Scanner
. Out of the box it gives you 1 answer per whitespace which is usually not what you want in the first place.