0

I want to catch any input that is not an Int and prompt for a valid input 3 times. After 3 times, the program will move on and ask for another rating. I can't seem to have the try/catch block repeat or even catch the InputMismatchException. Any suggestions?

do {
    //prompt for rating
        try {
        System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
        //capture
            rating = response.nextInt();
        } catch (InputMismatchException err) {
            System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
            rating = response.nextInt();
        } //end catch
    } while (rating >= 0);
1
  • Add a counter of some kind outside of the do-while loop, intiialised to 0, in the catch block, increment the counter and set the rating to -2. Commented Apr 23, 2015 at 3:46

1 Answer 1

1

You could either do it in a loop:

int count = 0;
do {
    //prompt for rating
        try {
        System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
        //capture
            rating = response.nextInt();
        } catch (InputMismatchException err) {
            System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
            count++;
        } //end catch
    } while (rating >= 0 && count < 3);

Or use a nested try/ catch:

do {
    //prompt for rating
        try {
        System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
        //capture
            rating = response.nextInt();
        } catch (InputMismatchException err) {
            System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
             try {
        System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
        //capture
            rating = response.nextInt();
        } catch (InputMismatchException err) {
            System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
            rating = response.nextInt();
        } //end catch
        } //end catch
    } while (rating >= 0);

Personally I would prefer the first method.

I tried this code and it ran without any Exception:

public class Main {

public static void main(String[] args) throws IOException {
    int count = 0;
    int rating = 0;
    do {
        Scanner response = new Scanner(System.in);
        //prompt for rating
            try {
            System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
            //capture
                rating = response.nextInt();

            } catch (InputMismatchException err) {
                System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
            } finally {
                count++;
                System.out.println("Rating-->" + rating);
            }
      } while (rating >= 0 && count < 3);

}

}

Sign up to request clarification or add additional context in comments.

3 Comments

My program still throws an InputMismatchException error even though I catch it and prompt for another rating. Any ideas as to why that is?
I did end up using the first suggestion though for prompting for it 3 times.
I just tried a piece of code and it ran without any exception.

Your Answer

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