Why does it work:
Scanner keyboard = new Scanner(System.in);
int i;
int beerBottles = 100;
System.out.println("How many bottles of beer are on the wall?");
while (!keyboard.hasNextInt())
{
System.out.println("Make sure you enter an integer.");
keyboard.next();
}
beerBottles = keyboard.nextInt();
I stumbled upon this, trying to make sure that user input was an integer without using try / catch, and I have no idea why it works or something is terribly wrong that I went missing. It seems to work fine, which would be great, but I don’t understand why the text “Make sure you enter an integer” is not always displayed. Can anyone explain?
source
share