import java.io.*; import java.util.*; public class SavitchIn { public static String readLine() { char nextChar; String result = ""; boolean done = false; while (!done) { nextChar = readChar(); if (nextChar == '\n') done = true; else if (nextChar == '\r') { // Do nothing. // Next loop iteration will detect '\n' } else result = result + nextChar; } return result; } public static String readLineWord() { String inputString = null, result = null; boolean done = false; while (!done) { inputString=readLine(); StringTokenizer wordSource = new StringTokenizer(inputString); if (wordSource.hasMoreTokens()) { result = wordSource.nextToken(); done = true; } else { System.out.println( "Your input is not correct. Your input must"); System.out.println( "contain at least one nonwhiteSpace character."); System.out.println( "Try again. Enter input:"); } } return result; } public static int readLineInt() { String inputString = null; int number = -9999; boolean done = false; while (!done) { try { inputString = readLine(); inputString = inputString.trim(); number = Integer.parseInt(inputString); done = true; } catch (NumberFormatException e) { System.out.println( "Your input number is not correct."); System.out.println("Your input number must be"); System.out.println("a whole number written as an"); System.out.println("ordinary numeral, such as 42" + "but do not use a plus sign."); System.out.println("Please try again."); System.out.println("Enter a whole number:"); } } return number; } public static long readLineLong() { String inputString = null; long number = -9999; boolean done = false; while (! done) try { inputString = readLine(); inputString = inputString.trim(); number = Long.parseLong(inputString); done = true; } catch (NumberFormatException e) { System.out.println("Your long integer has a bad format."); System.out.println( "Whole number, minus signs are ok, but no plus sign."); System.out.println("Enter a whole number."); } return number; } public static double readLineDouble() { String inputString = null; double number = -9999; boolean done = false; while (!done) { try { inputString = readLine(); inputString = inputString.trim(); number = Double.parseDouble(inputString); done = true; } catch (NumberFormatException e) { System.out.println( "Badly formatted double input number."); System.out.println("Enter the number:"); } } return number; } public static float readLineFloat() { String inputString = null; float number = -9999; boolean done = false; while (! done) { try { inputString = readLine(); inputString = inputString.trim(); number = Float.parseFloat(inputString); done = true; } catch (NumberFormatException e) { System.out.println( "Badly formatted float input."); System.out.println("Enter the number again:"); } } return number; } public static char readLineNonwhiteChar() { boolean done = false; String inputString = null; char nonWhite = ' '; while (!done) { inputString = readLine(); inputString = inputString.trim(); if (inputString.length() == 0) { System.out.println( "Your input must contain a non-white character"); System.out.println("Enter input:"); } else { nonWhite = (inputString.charAt(0)); done = true; } } return nonWhite; } public static char readChar() { int charAsInt = -1; // To keep the compiler happy try { charAsInt = System.in.read(); } catch(IOException e) { System.out.println(e.getMessage()); System.out.println("Fatal error. Ending program."); System.exit(0); } return (char) charAsInt; } public static boolean readLineBoolean() { boolean done = false; String inputString = null; boolean result = false; while (!done) { inputString = readLine(); inputString = inputString.trim(); if (inputString.equalsIgnoreCase("true") || inputString.equalsIgnoreCase("t")) { result = true; done = true; } else if (inputString.equalsIgnoreCase("false") || inputString.equalsIgnoreCase("f")) { result = false; done = true; } else { System.out.println( "Your boolean input is badly formatted."); System.out.println("Enter input:"); } } return result; } public static char readNonwhiteChar() { char next; next = readChar(); while (Character.isWhitespace(next)) next = readChar(); return next; } }