/** * Reads an integer from the user via getInt. Continues prompting the user until they enter an * integer within the specified range. * * @param prompt prompt for getInt() * @param lower lower bound for input (inclusive) * @param upper upper bound for input (inclusive) * @return an integer between lower and upper inclusive * <p>NOTE: behavior when lower is larger than upper is undefined, but will probably result in * a non-terminating loop... so don't do that. */ public static int getIntInRange(String prompt, int lower, int upper) { int userInput = getInt(prompt); Logger.log( Loglevel.PEDANTIC, "Getting int from user in range " + lower + "-" + upper + " (inclusive)"); while (userInput < lower || userInput > upper) { Logger.log( Loglevel.WARNING, "Error, input is not in range " + lower + "-" + upper + " (inclusive)"); userInput = getInt(prompt); } return userInput; }
/** * Read the lines from a file, returning them as a list of Strings, one line per index. * * @param filepath absolute or relative path to file * @return the lines from the file */ public static LinkedList<String> readLines(String filepath) { Logger.log(Loglevel.PEDANTIC, "reading lines from file " + filepath); LinkedList<String> lines = new LinkedList<String>(); try { Scanner stream = new Scanner(new File(filepath)); while (stream.hasNextLine()) { String next = stream.nextLine(); Logger.log(Loglevel.OVERKILL, "read line from file: " + next); lines.append(next); } } catch (Exception ex) { Logger.log(Loglevel.WARNING, "could not read file, exception: " + ex); } return lines; }
/** * Read a string form the user, displaying the prompt first. If a newline is desired after the * prompt, it must be included in the prompt string. * * @param prompt the prompt to display to the user * @return the user input as a string */ public static String getString(String prompt) { Logger.log(Loglevel.PEDANTIC, "get String from user with prompt " + prompt); String userInput; Scanner in = new Scanner(System.in); System.out.print(prompt); try { userInput = in.nextLine(); } catch (InputMismatchException e) { Logger.log(Loglevel.WARNING, "input invalid; cannot be cast to String"); Logger.log(Loglevel.WARNING, "encountered non-fatal exception: " + e); in.next(); // get rid of invalid value so we can try again userInput = getString(prompt); } Logger.log(Loglevel.PEDANTIC, "read String from user: " + userInput); return userInput; }
/** * Write lines into a file, given as a LinkedList of Strings, with one String per line. * * @param lines list of lines to write to the file * @param filepath absolute or relative path to the file */ public static void writeLines(LinkedList<String> lines, String filepath) { Logger.log(Loglevel.PEDANTIC, "writing lines to file " + filepath); try { FileOutputStream outStream = new FileOutputStream(filepath); BufferedWriter outStreamWriter = new BufferedWriter(new OutputStreamWriter(outStream)); for (String line : lines) { Logger.log(Loglevel.OVERKILL, "writing line: " + line); outStreamWriter.write(line + "\n"); } outStreamWriter.close(); outStream.close(); } catch (FileNotFoundException ex) { Logger.log(Loglevel.WARNING, "encountered exception while " + "writing to file: " + ex); } catch (IOException ex) { Logger.log(Loglevel.WARNING, "encountered exception while " + "writing to file: " + ex); } }
/** * Identical behavior to getString(String prompt), however, this method will continue prompting * the user until they enter a string which matches the given regex, and will not return until the * user enters a matching string. * * @param prompt the prompt to display to the user * @param regex the regular expression to match * @return {the user input as a string */ public static String getString(String prompt, String regex) { Logger.log(Loglevel.PEDANTIC, "get String from user with prompt " + prompt); Logger.log(Loglevel.PEDANTIC, "user input will be required to " + "match the regex: " + regex); String userInput; Scanner in = new Scanner(System.in); System.out.print(prompt); try { userInput = in.nextLine(); } catch (InputMismatchException e) { Logger.log(Loglevel.WARNING, "input invalid; cannot be cast to String"); Logger.log(Loglevel.WARNING, "encountered non-fatal exception: " + e); in.next(); // get rid of invalid value so we can try again userInput = getString(prompt); } if (!userInput.matches(regex)) { Logger.log( Loglevel.WARNING, "invalid input; input was a valid string, but does not meet " + "required input format"); userInput = getString(prompt, regex); } Logger.log(Loglevel.PEDANTIC, "read String from user: " + userInput); return userInput; }
/** * Asks the user a yes/no or true/false question. Will accept yes, y, true, or t as positive * input, and will accept no, n, f, or false as negative input. Any other input will result in a * repetition of the prompt. Use input is case insensitive. * * @param prompt The prompt to display to the user * @return true if positive input, false otherwise */ public static boolean getBoolean(String prompt) { String response = getString(prompt); if (response.equalsIgnoreCase("yes") || response.equalsIgnoreCase("y") || response.equalsIgnoreCase("true") || response.equalsIgnoreCase("t")) { return true; } else if (response.equalsIgnoreCase("no") || response.equalsIgnoreCase("n") || response.equalsIgnoreCase("false") || response.equalsIgnoreCase("f")) { return false; } else { Logger.log(Loglevel.WARNING, "invalid input; expected yes or no"); return getBoolean(prompt); } }