private static void errorMessage(String message, String expecting) { // Report error on input. if (readingStandardInput && writingStandardOutput) { // inform user of error and force user to re-enter. out.println(); out.print(" *** Error in input: " + message + "\n"); out.print(" *** Expecting: " + expecting + "\n"); out.print(" *** Discarding Input: "); if (lookChar() == '\n') out.print("(end-of-line)\n\n"); else { while (lookChar() != '\n') // Discard and echo remaining chars on the current line of input. out.print(readChar()); out.print("\n\n"); } out.print("Please re-enter: "); out.flush(); readChar(); // discard the end-of-line character inputErrorCount++; if (inputErrorCount >= 10) throw new IllegalArgumentException( "Too many input consecutive input errors on standard input."); } else if (inputFileName != null) throw new IllegalArgumentException( "Error while reading from file \"" + inputFileName + "\":\n" + message + "\nExpecting " + expecting); else throw new IllegalArgumentException( "Error while reading from inptu stream:\n" + message + "\nExpecting " + expecting); }
/** * Skips over any whitespace characters, except for end-of-lines. After this method is called, the * next input character is either an end-of-line, an end-of-file, or a non-whitespace character. * This method never causes an error. (Ordinarily, end-of-file is not possible when reading from * standard input.) */ public static void skipBlanks() { char ch = lookChar(); while (ch != EOF && ch != '\n' && Character.isWhitespace(ch)) { readChar(); ch = lookChar(); } }
/** * Skips over any whitespace characters, including for end-of-lines. After this method is called, * the next input character is either an end-of-file or a non-whitespace character. This method * never causes an error. (Ordinarily, end-of-file is not possible when reading from standard * input.) */ private static void skipWhitespace() { char ch = lookChar(); while (ch != EOF && Character.isWhitespace(ch)) { readChar(); if (ch == '\n' && readingStandardInput && writingStandardOutput) { out.print("? "); out.flush(); } ch = lookChar(); } }