import java.util.Scanner;
import java.util.InputMismatchException;
Illustrates the need for exceptions to handle input such as: 7, seven, and dividing by 0
public class DivideTwoNumbers_02{
public static int getInt(Scanner scanner, String prompt){
boolean success = false;
int num = 0;
while(success != true){
System.out.print(prompt);
try{
num = scanner.nextInt();
success = true;
}catch(InputMismatchException e){
System.err.println("Error, some error message of your choice:" + e);
String garbage = scanner.next();
System.err.println("DEBUGGING: garbage: " + garbage);
}
}
return num;
}
public static void main( String[] args ){
Scanner stdinScanner = new Scanner( System.in );
int num1 = 0;
int num2 = 0;
int result = 0;
boolean success = false;
while(success != true){
num1 = getInt( stdinScanner, "Please enter a number: ");
num2 = getInt( stdinScanner, "Please enter another number: ");
try{
result = num1 / num2;
success = true;
}catch(ArithmeticException e){
System.err.println("Can not divide by zero");
System.err.println();
}
}
System.out.println( num1 + " divided by " + num2 + " equals " + result);
}
}