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 DivideTwoNumbers01{

    public static int getInt(Scanner scanner, String prompt){
        do{
            System.out.print(prompt);
            try{
                return scanner.nextInt();
            }catch(InputMismatchException e){
                System.err.println("Sorry, there was an error processing your input ("+e.toString()+")");
                String garbage = scanner.next(); // clear out the bad input from the stream
                System.err.println("DEBUGGING: garbage: " + garbage);
            }
        }while(true);
    }

    public static void main( String[] args ){
        Scanner stdinScanner = new Scanner( System.in );
        int num1 = 0;
        int num2 = 0;
        int result = 0;

        boolean successfullyDivided = false;
        do{
            // get two numbers from the user
            num1 = getInt( stdinScanner, "Please enter a number: ");
            num2 = getInt( stdinScanner, "Please enter another number: ");

            try{
                // calculate the result
                result = num1 / num2;
                successfullyDivided = true;
            }catch(ArithmeticException e){
                System.err.println("Sorry, can't divide by zero");
            }
        }while(successfullyDivided == false);

        // display the result
        System.out.println( num1 + " divided by " + num2 + " equals " + result);
    }
}