Handled an exception for a previously written Java applet

Source: Internet
Author: User

Prior to the use of Java 0-99 of the number and translation between English, the input number will be translated into English, input English will be translated into numbers, such as input 56 output fiftysix input Fiftysix output 56,

Found that this will be an exception, when the input does not conform to the format, such as English and digital mixed, or input punctuation will be reported numberformatexception exception, and now catch the exception, by the way

Input out of bounds also added an exception, such as input 100 times "input out of bounds!" "。

The following code is attached:

Package translateclass;
Import Java.util.Scanner;


Abstract class inter
{
 string x[]={"Zero", "one", "one", "three", "four", "five", "six", "Seven", " Eight "," Nine "};
 string y[]={"Ten", "Eleven", "Twelve", "Thirteen", "fourteen", "fifteen", "Sixteen", "Seventeen", "eighteen", "Nineteen"};
 string z[]={"Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
 abstract void Translate () throws outexception;
}
Class Outexception extends Exception
{
 outexception (String msg)
 {
   Super (MSG);
 }
}

Class Transenglish extends inter//numbers translated into English
{
int A;

Transenglish (int a)
{
This.a=a;
}
public void Translate () throws Outexception
{
if (a>=0&&a<=9)
System.out.println (A + "translated into English: \ n" +x[a]);
else if (a>9&&a<=19)
System.out.println (A + "translated into English: \ n" +y[a%10]);
else if (a>19&&a<=99)
System.out.println (A + "translated into English: \ n" +z[a/10-2]+ "-" +x[a%10]);
Else
throw new Outexception ("Input number" +a+ "is not between 0-99!) ");

}

}
Class Transcout extends inter//English translation into numbers
{
String A;
int i;
int j=0;
Transcout (String a)
{
This.a=a;
}
public void Translate () throws Outexception
{



for (i=0;i<=7;i++)
{
if (A.contains (Z[i]))
j= (i+2) *10;
if (A==z[i])
{
System.out.println (A + "translated into numbers \ n" + (i+2) *10);
Return
}
}
for (i=0;i<=9;i++)
{
if (A==y[i])
{
System.out.println (A + "translated into numbers \ n" +i+10);
Return
}
else if (A.contains (X[i]))
{
System.out.println (A + "translated into numbers \ n" + (j+i));
Return
}
}
throw new Outexception ("Input English" +a+ "is not between zero and Ninety-nie!) ");
}
}
Class Translate
{
Private Translate () {}
private static Translate s= new Translate ();
public static Translate Gettranslate ()
{
return s;
}

public void Trans ()
{
System.out.print ("Please enter the number you want to translate or English (only 0-99):");
Scanner scanf=new Scanner (system.in);
try//Resolving input format exceptions
{
String Str=scanf.next ();
if (Str.charat (0) >=48&&str.charat (0) <=57)
{
int Cot=integer.parseint (STR);
New Transenglish (COT). Translate ();
}
Else
New Transcout (STR). Translate ();

}
catch (NumberFormatException e)
{
SYSTEM.OUT.PRINTLN ("Input Error! ");
System.out.println (E.tostring ());

}
catch (Outexception O)
{
SYSTEM.OUT.PRINTLN ("Input out of bounds! ");
System.out.println (O.tostring ());
}
Finally
{
System.out.println ("translation ends.") ");//Disconnect the server
}

}
public static void Main (string[] args)
{
Translate T = Translate.gettranslate ();
T.trans ();
New Translate (). Trans ();
}
}

Handled an exception for a previously written Java applet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.