private static long readInteger( long min, long max) { // read long integer, limited to specified range long x = 0; while (true) { String s = readIntegerString(); if (s == null) { errorMessage( "Integer value not found in input.", "Integer in the range " + min + " to " + max); } else { String str = s.toString(); try { x = Long.parseLong(str); } catch (NumberFormatException e) { errorMessage( "Illegal integer input, " + str + ".", "Integer in the range " + min + " to " + max); continue; } if (x < min || x > max) { errorMessage( "Integer input outside of legal range, " + str + ".", "Integer in the range " + min + " to " + max); continue; } break; } } inputErrorCount = 0; return x; }
/** * Skips whitespace characters and then reads a value of type double from input. Any additional * characters on the current line of input are retained, and will be read by the next input * operation. When using standard IO, this will not produce an error; the user will be prompted * repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException * will be thrown if a legal value is not found. */ public static double getDouble() { double x = 0.0; while (true) { String str = readRealString(); if (str == null) { errorMessage( "Floating point number not found.", "Real number in the range " + (-Double.MAX_VALUE) + " to " + Double.MAX_VALUE); } else { try { x = Double.parseDouble(str); } catch (NumberFormatException e) { errorMessage( "Illegal floating point input, " + str + ".", "Real number in the range " + (-Double.MAX_VALUE) + " to " + Double.MAX_VALUE); continue; } if (Double.isInfinite(x)) { errorMessage( "Floating point input outside of legal range, " + str + ".", "Real number in the range " + (-Double.MAX_VALUE) + " to " + Double.MAX_VALUE); continue; } break; } } inputErrorCount = 0; return x; }
/** * Skips whitespace characters and then reads a value of type boolean from input. Any additional * characters on the current line of input are retained, and will be read by the next input * operation. When using standard IO, this will not produce an error; the user will be prompted * repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException * will be thrown if a legal value is not found. * * <p>Legal inputs for a boolean input are: true, t, yes, y, 1, false, f, no, n, and 0; letters * can be either upper case or lower case. One "word" of input is read, using the getWord() * method, and it must be one of these; note that the "word" must be terminated by a whitespace * character (or end-of-file). */ public static boolean getBoolean() { boolean ans = false; while (true) { String s = getWord(); if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t") || s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") || s.equals("1")) { ans = true; break; } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("f") || s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") || s.equals("0")) { ans = false; break; } else errorMessage( "Illegal boolean input value.", "one of: true, false, t, f, yes, no, y, n, 0, or 1"); } inputErrorCount = 0; return ans; }