int var;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext() && scanner.hasNextLine()) {
System.out.printf("Enter an integer: ");
try {
var = scanner.nextInt();
} catch (InputMismatchException e) {
scanner.nextLine();
} catch (NoSuchElementException e) {
// TODO: how to handle CTRL+D?
}
}
scanner.close();