/** * Provides a variety of methods to prompt a user for input. * * @author Robert C. Duvall */ public class Prompter { ////////////////////////////////////////////////////////////// // state private Scanner myInput; ////////////////////////////////////////////////////////////// // constructors /** * Create Prompter for that reads user's data from console. */ public Prompter () { this(new Scanner()); } /** * Create Prompter for that reads user's data from input. * * @param input Scanner that reads delimited data */ public Prompter (Scanner input) { myInput = input; myInput.useDelimiter("\n"); } ////////////////////////////////////////////////////////////// // accessors /** * Reads next whole number value without prompting. * * @return value entered by user */ public int readInt () { return (new Scanner(readLine())).nextInt(); } /** * Reads next real number value without prompting. * * @return value entered by user */ public double readDouble () { return (new Scanner(readLine())).nextDouble(); } /** * Reads next string without prompting. * * @return value entered by user */ public String readString () { return (new Scanner(readLine())).nextString(); } /** * Reads next string value that ends with newline without prompting. * * @return value entered by user */ public String readLine () { return myInput.nextString(); } /** * Prompts user for data using prompt, then reads next whole number value. * * @param prompt text to display to user asking for input * * @return value entered by user */ public int promptInt (String prompt) { System.out.print(prompt); return readInt(); } /** * Prompts user for data using prompt, then reads next real number value. * * @param prompt text to display to user asking for input * * @return value entered by user */ public double promptDouble (String prompt) { System.out.print(prompt); return readDouble(); } /** * Prompts user for data using prompt, then reads next string value. * * @param prompt text to display to user asking for input * * @return value entered by user */ public String promptString (String prompt) { System.out.print(prompt); return readString(); } /** * Prompts user for data using prompt, then reads next string value that ends with new line. * * @param prompt text to display to user asking for input * * @return value entered by user */ public String promptLine (String prompt) { System.out.print(prompt); return readLine(); } /** * Prompts user for data using prompt, then reads next whole number value * that falls within range of low and hi inclusive. * * @param prompt text to display to user asking for input * @param low smallest possible value user can enter * @param hi largest possible value user can enter * * @return value entered by user */ public int promptRange (String prompt, int low, int hi) { int answer; do { answer = promptInt(prompt + " between " + low + " and " + hi + "? "); } while (low > answer || answer > hi); return answer; } /** * Prompts user for data using prompt, then reads next real number value * that falls within range of low and hi inclusive. * * @param prompt text to display to user asking for input * @param low smallest possible value user can enter * @param hi largest possible value user can enter * * @return value entered by user */ public double promptRange (String prompt, double low, double hi) { double answer; do { answer = promptDouble(prompt + " between " + low + " and " + hi + "? "); } while (low > answer || answer > hi); return answer; } /** * Prompts user for data using prompt, expected to be a yes/no question, * then returns true if user answered yes, false otherwise * * @param prompt yes/no question to display to user asking for input */ public boolean promptYesNo (String prompt) { String answer; do { answer = promptString(prompt + " (yes or no)? "); } while (!answer.equals("yes") && !answer.equals("no")); return (answer.equals("yes")); } ////////////////////////////////////////////////////////////// // Main // Allow this class to be run directly by Java. // Read data from user and echo it back until user enters just a "." public static void main (String args[]) { Prompter prompter = new Prompter(); // read number at a time int num; do { num = prompter.readInt(); System.out.print("**" + num + "**\n"); } while (num != 0); // read word at a time String word; do { word = prompter.readString(); System.out.print("**" + word + "**\n"); } while (! word.equals(".")); // read a line at a time while (true) { String response = prompter.promptLine("Enter keywords to search for: "); if (response.equals(".")) { break; } else { System.out.print("**" + response + "**\n"); } } } }