I have implemented a catch statement for both the InputMismatchException and the ArithmeticException, how come they are not working?

Also, if the user enters "quit" for the numerator, the program should be able to exit. But my understanding is that as soon as the user enters something that is not an integer, the program goes to catch the exception. So, how can the "quit" option be included here?

The below is my program. Thanks!

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

/**
 *
 */

/**
 * @author cathy
 *
 */
public class IntegerDivision {

	/**
	 * @param args
	 */

	static double result;

	public static double division (int numerator, int divisor) {

		result = numerator/divisor;

		return result;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int numerator=0, divisor=0;
		Scanner sc = new Scanner (System.in);

		boolean checkException = false;

		do {
			System.out.println("Enter the numerator (or 'quit'): ");
			try {
				numerator = sc.nextInt();
				checkException = true;
			}
			catch (InputMismatchException e)
			{
				//if the user enters 'quit' then quit program, if else then do the following
				System.out.println("Invalid data. Please enter an integer.");
				sc.next();
			}
		} while (checkException == false);

		checkException = true;

		do {
			System.out.println("Enter the divisor: ");
			try {
				divisor = sc.nextInt();
				checkException = true;
			}
			catch (InputMismatchException e)
			{
				System.out.println("Invalid data. Please enter an integer.");
				sc.next();
			}
			catch (ArithmeticException e)
			{
				System.out.println("Invalid data. Please enter an integer other than 0.");
				sc.next();
			}
		} while (checkException == false);

		result= division (numerator, divisor);

		System.out.println(numerator+"/"+divisor+"="+result);


	}

}

To catch all exceptions add a further catch(Exception x) following the last catch you currently have.

Please execute the program, copy all of the screen for your test and paste it here.

Where does your code test if the user input a 'q'?

To catch all exceptions add a further catch(Exception x) following the last catch you currently have.

I added the catch all, but there is still the error when I type in something like "g", or "0".

Also, what is the difference between catch(Exception x) and catch(Exception e)?

Thanks!

Member Avatar for coil

Try letting the user enter anything with [scanner].next(), and then parsing the String into an int in a try-catch. If the catch is triggered, then check the original input to see if "quit" was entered.

Some pseudo-code:

String s=<scanner variable>.next();
int numerator=0;

try {
	numerator=Integer.parseInt(s);
} catch (Exception e) {
	if(s.equals("quit"))
		<quit the program>
	else
		<error message>
}

EDIT: (Exception e) & (Exception x) are the same thing, they just use different variable names.

there is still the error

Please copy the full text and paste here.

on your first try statement, you don't catch the ArithmeticException

Do you want this that when you enter 'quit' your program ends instead of asking for "enter an integer".

Please copy the full text and paste here.

Right now, everything works fine except for catching the 0 division.

Here is the error display:
Enter the numerator (or 'quit'):
f
Invalid data. Please enter an integer.
Enter the numerator (or 'quit'):
8
Enter the divisor:
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at IntegerDivision.division(IntegerDivision.java:22)
at IntegerDivision.main(IntegerDivision.java:91)
------------------------------------------------------
Here is my current program code

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

/**
 *
 */

/**
 * @author cathy
 *
 */
public class IntegerDivision {

	/**
	 * @param args
	 */

	static double result;

	public static double division (int numerator, int divisor) {

		result = numerator/divisor;

		return result;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int numerator=0, divisor=0;
		String numString=null;
		Scanner sc = new Scanner (System.in);

		boolean checkException = false;

		do {
			System.out.println("Enter the numerator (or 'quit'): ");
			try {
				numString = sc.next();
				numerator=Integer.parseInt(numString);
				checkException = true;
			}
			catch (InputMismatchException e)
			{
				//if the user enters 'quit' then quit program, if else then do the following
				if ((numString).equals("quit"))
				{
					System.out.println("Quit...");
					System.exit(0);
				}

				System.out.println("Invalid data. Please enter an integer.");
				sc.next();
				checkException = false;
			}
			catch (NumberFormatException a)
			{
				//if the user enters 'quit' then quit program, if else then do the following
				if ((numString).equals("quit"))
				{
					System.out.println("Quit...");
					System.exit(0);
				}
				System.out.println("Invalid data. Please enter an integer.");
				sc.next();
				checkException = false;
			}
		} while (checkException == false);

		checkException = true;

		do {
			System.out.println("Enter the divisor: ");
			try {
				divisor = sc.nextInt();
				checkException = true;
			}
			catch (InputMismatchException e)
			{
				System.out.println("Invalid data. Please enter an integer.");
				sc.next();
				checkException = false;
			}
			catch (ArithmeticException a)
			{
				System.out.println("Invalid data. Please enter an integer other than 0.");
				sc.next();
				checkException = false;
			}
		} while (checkException == false);

		result= division (numerator, divisor);

		System.out.println(numerator+"/"+divisor+"="+result);


	}

}

Please copy the full text and paste here.

Ohh. I get it now. I am catching the wrong part of the program. I am not supposed to be catching the input. But catching the division.

:$

You know how to use the try{}catch block. Where in your code do you want to catch the div by 0 error? In division or by the caller of division.

Or your code code test if the divisor is 0 and prompt the user for a better one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.