Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program, called ExceptionalDivide, that asks the user for two integer va

ID: 3916626 • Letter: W

Question

Write a program, called ExceptionalDivide, that asks the user for two integer values and displays the quotient of the first value divided by the second value. Make sure your program reads in the two input values as ints. Your program must catch either of two exceptions that might arise and crash your program: java.util. InputMismatchException /wrong data type inputted java.lang.ArithmeticException // divide by zero error Once you catch these exceptions, your program must print out a message explaining what happened and then ask the user to re-enter both inputs. Also, the program must catch these exceptions repeatedly if the user keeps inputting invalid values. When valid values are entered, the program prints the quotient and ends. Write the entire program, including the class statement, the main method declaration, and any import statements that you may need.

Explanation / Answer

import java.util.InputMismatchException; import java.util.Scanner; public class ExceptionalDivide { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n1, n2, result; while (true) { try { System.out.print("Enter 2 integers: "); n1 = in.nextInt(); n2 = in.nextInt(); result = n1 / n2; System.out.printf("%d / %d is %d ", n1, n2, result); break; } catch (InputMismatchException e) { System.out.println("Invalid input"); } catch (ArithmeticException e) { System.out.println("divide by zero error"); } } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote