2

I am creating a simple program using the java language which uses a bunch of similar methods to retrieve information from the user. The method i have used to deal with the user entering invalid data seems very incorrect to me, so i am seeking professional guidance on how to better handle invalid data.

I have attempted to search for similar questions but have found none.

This is a sample of one of my methods:

public static int getInput()
{
    int temp = 1;

    do
    {
        System.out.println("Answers must be between 1 and 15");
        temp =  reader.nextInt();

        if(temp >=1 && temp <= 15)
        {
            return temp;
        }
        else
        {
            System.out.println("Please enter a valid value");
        }
    }while(temp > 15 || temp < 1);

    //This value will never be reached because the do while loop structure will not end until a valid return value is determined
    return 1;
}//End of getInput method

Is there a better way to write this method?

This question is entirely made up so i can learn a better method to implement in my future programs.

Is using a labeled break statement acceptable? such as:

public static int getInput()
{
    int temp = 1;

    start:

        System.out.println("Answers must be between 1 and 15");
        temp =  reader.nextInt();

        if(temp >=1 && temp <= 15)
        {
            return temp;
        }
        else
        {
            System.out.println("Please enter a valid value");
            break start;
        }


}

Thank you very much in advance.

Singh
  • 301
  • 2
  • 6
  • 16
  • looks fine. btw, why don't you use GUI for this? awt/swt/swing....? In that case, you have many options. One is InputVerifier - http://stackoverflow.com/questions/12997742/java-swing-implementing-a-validy-check-of-input-values – Jayan Jun 05 '13 at 06:23
  • @Jayan I am just trying to learn a new concept; GUI isn't required to illustrate the question. There must be a better way to do this, the last return statement is never reached, and temp is checked if it is within the allowed values twice... edit: Sorry i didn't notice the last sentence; I would like to do this without GUI. – Singh Jun 05 '13 at 06:25

2 Answers2

2

You have forgotten to check the case, that non-number values are entered (Scanner#nextInt throws a java.util.InputMismatchException). One suggestion which takes care of that issue, is less redundant and more flexible:

public static int getInput(int min, int max) {
    for (;;) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(String.format("Answers must be between %s and %s", min, max));
        try {
            int value = scanner.nextInt();
            if (min <= value && value <= max) {
                return value;
            } else {
                System.out.println("Please enter a valid value");
            }
        } catch (InputMismatchException e) {
            System.out.println("Input was no number");
        }
    }
}
qqilihq
  • 10,794
  • 7
  • 48
  • 89
  • Thanks for your answer. I liked the use of "for(;;){}" i did not know that was possible. :) – Singh Jun 05 '13 at 06:47
  • I have a problem; when the InputMismatchException is caught, the "input was no number" message is infinitely printed out... How do i prevent this? – Singh Jun 05 '13 at 12:26
  • Strange, works fine for me, just checked. Make sure to instantiate the Scanner *inside* the loop. – qqilihq Jun 05 '13 at 16:05
  • Ohhh okay, that was my mistake. Thanks again! Is there any way to avoid having to instantiate the scanner inside the loop? i am asking because i have like 12 methods similar to this; is it bad to have scanner instantiated in each one of them? – Singh Jun 07 '13 at 05:09
0

If you are just worried about the return that is not used and double checking temp you can do something like

public static int getInput()
{
    while(true)
    {
        System.out.println("Answers must be between 1 and 15");
        temp = reader.nextInt();

        if(temp >=1 && temp <= 15)
        {
            return temp;
        }
        else
        {
            System.out.println("Please enter a valid value");
        }
    }
}//End of getInput method
Dan675
  • 1,697
  • 15
  • 15