Home > database >  Catching exceptions on multiple inputs from user but not go back to first input
Catching exceptions on multiple inputs from user but not go back to first input

Time:11-18

Here is a small program that has multiple inputs from the user to get the smallest number entered. I'm trying to catch the InputMismatchException all in the same try/catch block but if the user inputs anything but an int on say the second or third int to be entered then it jumps back to the first input which the user already entered correctly. Is there a simpler way to fix this or do I need to create separate try/catch blocks for each input?

public class GrabbingSmallestValueEntered {

public static void main(String[] args) {
    
    //Scanner input = new Scanner(System.in);
    boolean correctInput = true;
    double a = 0;
    double b = 0;
    double c = 0;

    Scanner input = new Scanner(System.in);

        do {
            
            try {
            
                System.out.println("Input the first number: ");
                a = input.nextDouble();
                
                System.out.println("Input the second number: ");
                b = input.nextDouble();
                
                System.out.println("Input the third number: ");
                c = input.nextDouble();
                correctInput = false;
            
        }catch (InputMismatchException ie) {
            System.out.println("Only numbers allowed to be entered. No letters or words!");
            input.nextLine();
        }

        
    } while (correctInput);
    
        System.out.println("The smallest value is "   smallest(a, b, c)   "\n");
        

}

CodePudding user response:

Make methods. You have a task: 'ask for a double, and if the user doesn't enter one, tell them, and keep asking until they do'. You want to run this task repeatedly and with different prompts.

Parameterize the differences and make a method. Then invoke that:

public static double askDouble(Scanner input, String prompt) {
  while (true) {
    try {
      System.out.println(prompt);
      return input.nextDouble();
    } catch (InputMismatchException e) {
      System.out.println("Only numbers allowed, etc...");
      input.next(); //don't mix nextLine and nextAnything else.
    }
  }
}

and then call that method in your main (no need for a do/while loop there, at all, or that correctInput boolean. In fact, your mine now becomes 3 very very simple lines. Yay methods!)

CodePudding user response:

You can declare an array of double, read the values in a loop, in the loop you try/catch the exception. In case there is no error you increment the index. It could be something like this :

double[] a = [0,0,0];
int i = 0;
do {
   try {
       System.out.println("Input the first number: ");
       a[i] = input.nextDouble();
       i  ;
   } catch (InputMismatchException ie) {
        System.out.println("Only numbers allowed to be entered. No letters or words!");
        input.nextLine();
    }
} while(i<3);

I don't have a compiler to test this, if there is an error let me know i'll try to help you.

CodePudding user response:

Is there a simpler way to fix this or do I need to create separate try/catch blocks for each input?

Yes. Instead of reading out using nextDouble(), just do ...

var input1Correct = false 
while(!input1Correct) {
  System.out.println("Input the first number: ");
  a = input.nextLine();
  input1Correct = validateInput(1)  
}

Then you do manual parsing and validation and if everything is OK, proceed to the next step. Otherwise, ask to reenter.

You won't even need the catch clause in this case.

Does this solve your problem ? Let me know in the comments.

  • Related