import java.util.Scanner; import java.util.InputMismatchException; public class RangeInput { RangeInput (int low, int high) { this.low = low; this.high = high; in = new Scanner (System.in); } public int readNumber() { int number; while (true) { try { System.out.print("Please enter a number between " + low + " and " + high + " > "); number = in.nextInt(); if ((number < low) || (number > high)) { throw (new NumberRangeException()); } return number; } catch (NumberRangeException exc) { System.out.println("Error - the number is either too small or too big."); } catch (InputMismatchException exc) { in.nextLine(); // must flush the input buffer // System.out.println(exc); System.out.println("Sorry - that must be an integer. Please try again."); } } } private int low; private int high; private Scanner in; }