I've looked at the documentation for the methods but I still can't quite understand how they work. Here are my questions: .hasNextInt() checks the next integer value and doesn't consume anything, yet in my code below the first time it appears it allows a user to enter a value; why is that? Afterwards I have to use .next() to clear the current value of the scanner object I'm using and will not be prompted for a new value if I remove this from the while loop.
As for .nextInt(), it's only on the first iteration in the code that it checks for the last integer value in the scanner object, but when I enter it again it allows me to enter in a new value. This all seems very inconsistent and I'm having a hard time understanding what's going on under the hood.
int integer=0;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an integer");
while(!scan.hasNextInt())
{
System.out.println("Wrong");
scan.next();
}
integer = scan.nextInt();
System.out.println(integer);
int newNumber = scan.nextInt();
System.out.println(newNumber);