
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class DivisionDemoFinalVersion
{
    public static void main(String[] args)
    {
        int numerator = inputInt("Enter numerator:");
        int denominator = 0; //to keep compiler happy
        boolean done = false;

        while (! done)
        {
            try
            {
                 denominator = inputInt("Enter denominator:");
                 double quotient = safeDivide(numerator, denominator);
                 done = true;
                 System.out.println(numerator + "/" + denominator 
                                        + " = " + quotient);
             }
             catch(DivisionByZeroException e)
             {
                 System.out.println("Cannot divide by zero.");
                 System.out.println("Try again.");
             }
        }

        System.out.println("End of program.");
    }

    public static double safeDivide(int top, int bottom) 
                                 throws DivisionByZeroException
    {
        if (bottom == 0)
            throw new DivisionByZeroException( );
        return top/(double)bottom;
    }

    public static int inputInt(String prompt) 
    {
        BufferedReader keyboard = new BufferedReader(
                           new InputStreamReader(System.in));
        String numeralString = null; //to keep compiler happy
        int inputValue = 0; //to keep compiler happy
        boolean done = false;

        while (! done)
        {
            try
            {
                System.out.println(prompt); 
                numeralString = keyboard.readLine( );
                inputValue = Integer.parseInt(numeralString);
                done = true;
            }
            catch(NumberFormatException e)
            {
                System.out.println(numeralString 
                                            + " is not a valid input.");
                System.out.println("Try again.");
            }
            catch(IOException e)
            {
                System.out.println("IO Problem. Aborting program.");
                System.exit(0);
            }
        }

      return inputValue;

   }

}

