Monday, February 21, 2011

console input in java

I wrote up a sample of nice and clean console input with validation, and repeated prompting on invalid inputs.

package org.yi.happy.console;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DemoInput {
    /**
     * A demo of prompting for and reading validated data from standard input.
     */
    public static void main(String[] args) {
        try {
            /*
             * get a usable line reader.
             */
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    System.in));

            /*
             * the result of the read loop
             */
            String word = null;
            /*
             * keep going until we get what we want
             */
            while (true) {
                /*
                 * ask the question
                 */
                System.out.print("enter a word: ");
                /*
                 * get the answer
                 */
                String line = in.readLine();
                /*
                 * is the answer what we want?
                 */
                if (line.matches("\\w+")) {
                    /*
                     * save the answer and done
                     */
                    word = line;
                    break;
                } else {
                    /*
                     * report a bad answer and repeat
                     */
                    System.out.println("that does not look like a word: "
                            + line);
                    continue;
                }
            }

            /*
             * the result of the read loop
             */
            double number = 0;
            /*
             * keep going until we get what we want
             */
            while (true) {
                /*
                 * ask the question
                 */
                System.out.print("enter a number: ");
                /*
                 * get the answer
                 */
                String line = in.readLine();
                /*
                 * parse the answer
                 */
                try {
                    number = Double.parseDouble(line);

                    /*
                     * range checks can go here (on failure do a continue, on
                     * accept do a break)
                     */

                    /*
                     * it was good, done
                     */
                    break;
                } catch (NumberFormatException e) {
                    /*
                     * it was not good, report the bad answer and repeat
                     */
                    System.out.println("that does not look like a number: "
                            + line);
                    continue;
                }
            }

            /*
             * if we get here we have two good answers.
             */

            /*
             * do something using the input
             */
            System.out.println("the word was " + word + " and the number was "
                    + number);

        } catch (IOException e) {
            /*
             * there was an error, so display it and give up
             */
            e.printStackTrace();
        }

    }
}

I don't think I can make it any simpler and still repeat the prompting for input until valid input is given. If there were many inputs being done I could break the logic out into an instance of the template pattern.

No comments:

Post a Comment