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");
}
}