HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Making sure user inputs correct type

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
inputsusersuretypecorrectmaking

Problem

I have two functions that are used to prompt and recieve input from the user. One is for int and the other is for boolean. They work somewhat similar to each other, one uses try/catch and the other doesn't. Can they be improved upon or is one better than the other? I'm having trouble with the user interface, I'm not sure what should happen when the user gives an invalid input. I asked on userexperience and didn't get much feed back.

/*returns integer to question asked in prompt*/
public static int getNumber(String prompt)
{
    Scanner sc = new Scanner(System.in);
    while (true) {
        System.out.print("\t"+prompt+": ");
        String input = sc.next();
        try {
            return Integer.parseInt(input);
        } catch (NumberFormatException ne) {
            System.out.println("\tPlease enter an integer.");
        }
    }
}

/*returns boolean to question asked in message*/
public static boolean getBool(String message)
{
    Scanner sc = new Scanner(System.in);
    String answer;

    System.out.print("\t"+message+": ");
    while (true) {
      answer = sc.nextLine().trim().toLowerCase();
      if (answer.equals("y")) {
        return true;
      } else if (answer.equals("n")) {
        return false;
      } else {
         System.out.print("\tPlease answer y/n");
      }
    }

}


One problem is "please enter an integer". If the user is non-technical they may not know what an integer is, but I can't think of better wording.

Here is another version of the methods

```
/returns integer to question asked in prompt/
public static int getNumber(String prompt)
{
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("\t"+prompt+": ");
String input = sc.next();
try {
return Integer.parseInt(input);
} catch (NumberFormatException ne) {
System.out.println("\tPlease enter an integer.");
}
}
}

/returns boolean to question asked in message/
public static

Solution

If the user is non-technical they may not know what an integer is, but
I can't think of better wording.

[Positive] Number or [Positive] Whole Number would be alternatives.

Another possibility: Enter a Number between 0 and [X]. Non-technical people will assume that this means they should enter a whole number. If they enter a different number, give feedback accordingly.

Consistency

In your first version: getNumber outputs the prompt each time, while getBool does not. This should be consistent. The names of the input variable should be consistent as well (either prompt or message. I would prefer prompt.

Other

You can do sc.nextInt(); instead of String input = sc.next(); Integer.parseInt(input);.

As already mentioned on UX, show the possible values for the boolean answer in the prompt when asking the question. So

boolean wind = getBool("Regularly windy");


becomes

boolean wind = getBool("Regularly windy (y/n)");


Extract Method

You can extract the method to retrieve a string. I think that this makes the code a bit more readable, and you get getString method for free.

/*returns integer to question asked in prompt*/
public static int getNumber(String prompt) {
    while (true) {
        String input = getString(prompt);
        try {
            return Integer.parseInt(input);
        } catch (NumberFormatException ne) {
            System.out.println("\tPlease enter a positive whole number.");
        }
    }
}

/*returns boolean to question asked in prompt*/
public static boolean getBool(String prompt) {
    while (true) {            
        String input = getString(prompt).trim().toLowerCase();
        if (input.equals("y")) {
            return true;
        } else if (input.equals("n")) {
            return false;
        } else {
            System.out.print("\tPlease answer y/n");
        }
    }
}

/*returns string to question asked in prompt*/
public static String getString(String prompt) {
    System.out.print("\t" + prompt + ": ");
    Scanner sc = new Scanner(System.in);
    return sc.nextLine();
}

Code Snippets

boolean wind = getBool("Regularly windy");
boolean wind = getBool("Regularly windy (y/n)");
/*returns integer to question asked in prompt*/
public static int getNumber(String prompt) {
    while (true) {
        String input = getString(prompt);
        try {
            return Integer.parseInt(input);
        } catch (NumberFormatException ne) {
            System.out.println("\tPlease enter a positive whole number.");
        }
    }
}

/*returns boolean to question asked in prompt*/
public static boolean getBool(String prompt) {
    while (true) {            
        String input = getString(prompt).trim().toLowerCase();
        if (input.equals("y")) {
            return true;
        } else if (input.equals("n")) {
            return false;
        } else {
            System.out.print("\tPlease answer y/n");
        }
    }
}

/*returns string to question asked in prompt*/
public static String getString(String prompt) {
    System.out.print("\t" + prompt + ": ");
    Scanner sc = new Scanner(System.in);
    return sc.nextLine();
}

Context

StackExchange Code Review Q#58800, answer score: 3

Revisions (0)

No revisions yet.