Why does this work:
Scanner keyboard = new Scanner(System.in); int i; int beerBottles = 100; //Asks the user for the number of beer bottles on the wall 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(); } //Sets beerBottles to the user entered value beerBottles = keyboard.nextInt();
I stumbled on this while trying to make sure that the user input was an integer without using try/catch and I have no idea why it works or if something is horrendously wrong that I'm missing. It seems to work perfectly, which would be great, but I don't understand why the "Make sure you enter an integer" text doesn't always show. Can anyone explain?