import java.util.Scanner; public class ReadInt { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter a number."); int number=0; //! = is not. So if input doesn't have a next int, the condition is true /* if(!input.hasNextInt()) { System.out.println("The entered number was not an integer"); } else { number = input.nextInt(); System.out.printf("You typed: %d \n", number); } */ while(!input.hasNextInt()) { System.out.println("That was not a number. Please enter a number."); input.nextLine(); //throw away whatever they typed. We know it is not an integer. } //We know the user has put in an integer number = input.nextInt(); System.out.printf("You typed: %d \n", number); } }