0

I am writing a simple division program. The User enter the numerator and the denominator. The numerator and denominator is to throw an exception if the user does not enter an integer. In addition, the denominator should throw an exception if the user enter 0. The error should tell the user each time what they did wrong and continue to loop until the correct input is entered. Then it should exit when the values are correct.

Why when the user put in incorrect entries for the denominator it request the numerator again?

import java.util.InputMismatchException;
import java.util.Scanner;

public static void main(String[] args) 
{
    // TODO Auto-generated method stub  
    System.out.printf("Welcome to Division \n");

    Scanner div = new Scanner(System.in);
    boolean continueLoop = true;
do
{
    try 
    {
    System.out.println("\nEnter a numerator:\n");
    int num1 = div.nextInt();
    System.out.println("Enter a denominator:");
    int num2 = div.nextInt();

    int result = quotient(num1, num2);
    System.out.printf("The Answer is: %d / %d = %d%n",num1,num2,result);
    }
    catch(InputMismatchException inputMismatchException)
    {
        System.err.printf("\n Exception \n",inputMismatchException );
        div.nextLine();
        System.out.printf("You must enter integers");

    }
    catch(ArithmeticException arithmeticException)
    {
        System.err.printf("\n Exception \n",arithmeticException);
        System.out.printf(" Zero is an invalid entry");
    }
} while (continueLoop);

    System.out.printf("\n GOODBYE \n");
}

}
1
  • You have a single loop, so it will go through all its steps each time it runs. Run your code step-by-step under debugger and see for yourself. Commented Jan 24, 2016 at 5:25

3 Answers 3

1

You catch the exceptions but the value of continueLoop is never updated

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

Comments

0

You need a nested while loop for that. That's an answer for your italicized question. It ask for the numerator again because your do while loop starts from entering the numerator.

2 Comments

I tried adding an if statement but it wont compile.
You have to start 2 do while loops. I do this in c++. The first, start do while and try with entering the numerator. After that is one catch. Then inside that do, put another do for entering denominator. Then another catch. Now you have write the perfect 2 while statements in the end. The first while isfor the denominator input, the loop will come back to that point if you have the correct while statement. The second while will start the loop from the top again. Now — if you tried to wrong the numerator input, it has to jump back to the top again.
0

If you want the program to stop when an exception is thrown you can do this:

     try {
            do {

                System.out.println("\nEnter a numerator:\n");
                int num1 = div.nextInt();
                System.out.println("Enter a denominator:");
                int num2 = div.nextInt();

                int result = quotient(num1, num2);
                System.out.printf("The Answer is: %d / %d = %d%n", num1, num2, result);
            } while (continueLoop);
        } catch (ArithmeticException arithmeticException) {
            System.err.printf("\n Exception \n", arithmeticException);
            System.out.printf(" Zero is an invalid entry");
        } catch (InputMissMatchException inputMismatchException) {
            System.err.printf("\n Exception \n", inputMismatchException);
            div.nextLine();
            System.out.printf("You must enter integers");

        }
        System.out.printf("\n GOODBYE \n");

But right now you catch is inside the while loop and your program prints the messages from the exceptions and continue again. If you want to continue this way you can enter a validation after the exception if the person wants to continue to run your program or something like that which you can connect to your continueLoop

2 Comments

Thank you. But I want it to advise the user to enter an integer for the denominator and not start all over
then you can make two separate try catch block and the one with the denominator should be in a do-while loop until a successfully entered. You can make a boolean for a valid input and in the catch block you show the user you message make valid = false. When it goes to the while condition it will see the boolean is false and will return to the denominator message again.

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.