1

I have simple java program that accepts 3 user inputs of type integer, double and string. I would like to know the best/most efficient way to perform error handling on all of these inputs in order to keep the program running, inform the user they have entered an incorrect input and ask them the question again . Any help would be greatly appreciated.

Here is my code

    Scanner scan = new Scanner(System.in);
    int inputInt;
    double inputDbl;
    String inputString;


    System.out.print("Please enter a whole number: ");
    inputInt = scan.nextInt();      
    System.out.print("Please enter a decimal number: ");
    inputDbl = scan.nextDouble();
    System.out.print("Please enter a string: ");
    inputString = scan.next().toLowerCase();    
2
  • 2
    Make sure you use the hasNextInt() and hasNextDouble() methods of scanner before calling nextInt(), nextDouble(). Commented Nov 5, 2015 at 11:59
  • Don't forget to accept the answer which fixed your problem. Commented Nov 5, 2015 at 12:36

3 Answers 3

3

Thanks for all the input people, you guys are awesome. I chose to just use a simple dowhile loop using a boolean trigger. I've tried using try catches before but ended up writing huge blocks of code to perform very basic input checks. So don't have any exception handling here, which I hope isn't gonna be necessary this way. Hopefully it doesn't break on me

do {
        System.out.print("Please enter a whole number: ");
        if (scan.hasNextInt()){     
        inputInt = scan.nextInt();
        validInput = true;
        } else
            System.out.println("You have entered incorrect input! Please enter a whole number only");
            scan.nextLine();
        } while (validInput == false);  

        validInput = false;

        do {
        System.out.print("Please enter a decimal number: ");
        ......
        ......
Sign up to request clarification or add additional context in comments.

Comments

2

Splitting this into n methods where n is how many user inputs there are.

For each user input create a method that gets the input:

String getStringInput(){
    System.out.println("Enter input");
    String input = scan.next();

    //check the input to make sure it is correct
    if(input.equals("foo")){
        //if the input is incorrect tell the user and get the new input
        System.out.println("Invalid Input");
        //simply return this method if the input is incorrect.
        return getStringInput();
    }

    //return the input if it is correct
    return input;
}

For the main method that gets the input simply call the method:

void getAll(){
    String stringValue = getStringInput();
}

This now makes it easy to get any number of inputs and check if the are correct.

Comments

1
boolean validated = false;

// this keeps user locked until he/she provides valid inputs for all variables
while(!validated) {
  //get inputs here
  // ..

  // lastly
  validated = validateLogic(inInt, inDbl, inStr);
}

// keep going

If you want to validate separately for each input, you shuold write while loop 3 times.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.