Saturday, March 7, 2009

User-Friendly Division by:Junnaliza Arreglado

//Programmer: Junnaliza Arreglado
//Date Started: March 6,2009
//Date Finished:March 7,2009
//Programme Name: User-Friendly Division
//Purpose: To use Exception handling in the Programme


import java.util.*; //importing the java util package that holds the InputMismatch and the Scanner class

public class jhong
{

public static int quo(int num, int div)throws ArithmeticException
{
return num/div; //holds the aithmetic exeption of the try block
}

public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num=0;
int div=0;
String quit="exit";
char x=quit.charAt(0); //String class initialization
while(x!='q' || x!='Q') //to initialize the quit of the program
{
if(x!='q' || x != 'Q') //to initialize the quit of the program
{
try //starting the try block
{
System.out.println("Enter the numerator:");
if((input.next().charAt(0)=='q')||(input.next().charAt(0)=='Q'))
{
x=input.next().charAt(0);
}
else
{
num=input.nextInt();
System.out.println("Enter the divisor:");
div=input.nextInt();
int finalQ=quo(num,div);
System.out.println(num+"/"+div+" is "+finalQ);
}
} //end of the try block


catch(ArithmeticException wrongDiv) //wrong denominator-the 0
{
System.err.println("You can't divide "+num+" by "+div); //error message
}

catch(InputMismatchException stringNum) //catch for wrong input of the numerator
{
System.err.println("You entered bad data.\nPlease try again."); //error message
}


}
else
{
x='q'; //quiting
}
}

}
}

No comments:

Post a Comment