import static java.lang.System.out; import java.util.Scanner; import java.util.InputMismatchException; public class ScannerTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean done = false; int n = 0; while (!done) { try { out.print("Please give me an int here: "); n = sc.nextInt(); // this can cause InputMismatchException done = true; } catch (InputMismatchException e) { out.println("That's not an int."); sc.nextLine(); // throw away junk in the current input line } } out.println("You entered " + n); sc.close(); } }