/** * Puts a GUI file-selection dialog box on the screen in which the user can select an input file. * If the user cancels the dialog instead of selecting a file, it is not considered an error, but * the return value of the subroutine is false. If the user does select a file, but there is an * error while trying to open the file, then an exception of type IllegalArgumentException is * thrown. Finally, if the user selects a file and it is successfully opened, then the return * value of the subroutine is true, and the input routines will read from the file, instead of * from standard input. If the user cancels, or if any error occurs, then the previous input * source is not changed. * * <p>NOTE: Calling this method starts a GUI user interface thread, which can continue to run even * if the thread that runs the main program ends. If you use this method in a non-GUI program, it * might be necessary to call System.exit(0) at the end of the main() routine to shut down the * Java virtual machine completely. */ public static boolean readUserSelectedFile() { if (fileDialog == null) fileDialog = new JFileChooser(); fileDialog.setDialogTitle("Select File for Input"); int option = fileDialog.showOpenDialog(null); if (option != JFileChooser.APPROVE_OPTION) return false; File selectedFile = fileDialog.getSelectedFile(); BufferedReader newin; try { newin = new BufferedReader(new FileReader(selectedFile)); } catch (Exception e) { throw new IllegalArgumentException( "Can't open file \"" + selectedFile.getName() + "\" for input.\n" + "(Error :" + e + ")"); } if (!readingStandardInput) { // close current file try { in.close(); } catch (Exception e) { } } emptyBuffer(); // Added November 2007 in = newin; inputFileName = selectedFile.getName(); readingStandardInput = false; inputErrorCount = 0; return true; }
/** * After this method is called, input will be read from inputStream, provided it is non-null. If * inputStream is null, then this method has the same effect as calling readStandardInput(); that * is, future input will come from the standard input stream. */ public static void readStream(Reader inputStream) { if (inputStream == null) readStandardInput(); else { if (inputStream instanceof BufferedReader) in = (BufferedReader) inputStream; else in = new BufferedReader(inputStream); emptyBuffer(); // Added November 2007 inputFileName = null; readingStandardInput = false; inputErrorCount = 0; } }
/** * After this method is called, input will be read from standard input (as it is in the default * state). If a file or stream was previously the input source, that file or stream is closed. */ public static void readStandardInput() { if (readingStandardInput) return; try { in.close(); } catch (Exception e) { } emptyBuffer(); // Added November 2007 in = standardInput; inputFileName = null; readingStandardInput = true; inputErrorCount = 0; }
/** * Opens a file with a specified name for input. If the file name is null, this has the same * effect as calling readStandardInput(); that is, input will be read from standard input. If an * error occurs while trying to open the file, an exception of type IllegalArgumentException is * thrown, and the input source is not changed. If the file is opened successfully, then after * this method is called, all of the input routines will read from the file, instead of from * standard input. */ public static void readFile(String fileName) { if (fileName == null) // Go back to reading standard input readStandardInput(); else { BufferedReader newin; try { newin = new BufferedReader(new FileReader(fileName)); } catch (Exception e) { throw new IllegalArgumentException( "Can't open file \"" + fileName + "\" for input.\n" + "(Error :" + e + ")"); } if (!readingStandardInput) { // close current input stream try { in.close(); } catch (Exception e) { } } emptyBuffer(); // Added November 2007 in = newin; readingStandardInput = false; inputErrorCount = 0; inputFileName = fileName; } }
/** * Skips whitespace characters and then reads one "word" from input, discarding the rest of the * current line of input (including the next end-of-line character, if any). A word is defined as * a sequence of non-whitespace characters (not just letters!). When using standard IO, this will * not produce an error. In other cases, an IllegalArgumentException will be thrown if an * end-of-file is encountered. */ public static String getlnWord() { String x = getWord(); emptyBuffer(); return x; }
/** * Skips whitespace characters and then reads a value of type boolean from input, discarding the * rest of the current line of input (including the next end-of-line character, if any). 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 getlnBoolean() { boolean x = getBoolean(); emptyBuffer(); return x; }
/** * Skips whitespace characters and then reads a value of type char from input, discarding the rest * of the current line of input (including the next end-of-line character, if any). Note that the * value that is returned will be a non-whitespace character; compare this with the getAnyChar() * method. When using standard IO, this will not produce an error. In other cases, an error can * occur if an end-of-file is encountered. */ public static char getlnChar() { char x = getChar(); emptyBuffer(); return x; }
/** * Skips whitespace characters and then reads a value of type double from input, discarding the * rest of the current line of input (including the next end-of-line character, if any). 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 getlnDouble() { double x = getDouble(); emptyBuffer(); return x; }
/** * Skips whitespace characters and then reads a value of type float from input, discarding the * rest of the current line of input (including the next end-of-line character, if any). 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 float getlnFloat() { float x = getFloat(); emptyBuffer(); return x; }
/** * Skips whitespace characters and then reads a value of type long from input, discarding the rest * of the current line of input (including the next end-of-line character, if any). 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 long getlnLong() { long x = getLong(); emptyBuffer(); return x; }
/** * Skips whitespace characters and then reads a value of type int from input, discarding the rest * of the current line of input (including the next end-of-line character, if any). 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 int getlnInt() { int x = getInt(); emptyBuffer(); return x; }
/** * Skips whitespace characters and then reads a value of type short from input, discarding the * rest of the current line of input (including the next end-of-line character, if any). 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 short getlnShort() { short x = getShort(); emptyBuffer(); return x; }
/** * Skips whitespace characters and then reads a value of type byte from input, discarding the rest * of the current line of input (including the next end-of-line character, if any). 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 byte getlnByte() { byte x = getByte(); emptyBuffer(); return x; }