Programmer: Gerlie O. Lidon
Program Name: User-Friendly Division
Time Started: March 6,2009
Time Finished: March 13, 2009
Program Purpose:To create a program about Exception Handling
import java.util.InputMismatchException;
import java.util.Scanner;
public class DivisionPractice
{
public static int quotient( int numerator, int denominator )
throws ArithmeticException
{
return numerator / denominator;
}
public static void main( String args[] )
{
Scanner scanner = new Scanner( System.in );
boolean continueLoop = true; // determines if more input is needed
{
{
System.out.print( "Enter your numerator: " );
int numerator = scanner.nextInt();
System.out.print( "Enter your denominator: " );
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
System.out.printf( "\nResult: %d / %d = %d\n", numerator,
denominator, result );
continueLoop = false;
}
catch ( InputMismatchException inputMismatchException )
{
System.err.printf( "\nException: %s\n", inputMismatchException ); // discard input so user can try again
scanner.nextLine();
System.out.println( "You must enter integers. Please try again.\n”);
}
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
System.out.println( "Zero is an invalid denominator. Please try again.\n" );
}
} while ( continueLoop );
}
}
Friday, March 13, 2009
Exception Handling( User-Friendly Division)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment