Java
Codes : Converting Numbers to Words
File Name: Kamikuto1.java
Description: This program uses Switch Statements
and Multiple Sub-Classes to covert numbers to words.
Output: Converting numbers to words.
Program:
public class Kamikuto1{
private double getPlace( String number ){ switch( number.length()
){
case 1:
return DefinePlace.UNITS;
case 2:
return DefinePlace.TENS;
case 3:
return DefinePlace.HUNDREDS;
case 4:
return DefinePlace.THOUSANDS;
case 5:
return DefinePlace.TENTHOUSANDS;
case 6:
return DefinePlace.MILLIONS; //return DefinePlace.LAKHS;
case 7:
return DefinePlace.TENMILLIONS; //return DefinePlace.TENLAKHS;
case 8:
return DefinePlace.BILLIONS; //return DefinePlace.CRORES;
case 9:
return DefinePlace.TENBILLIONS; //return DefinePlace.TENCRORES;
}//switch return 0.0; }// getPlace
private String getWord( int number ){ switch( number ){
case 1:
return "One";
case 2:
return "Two";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
case 0:
return "Zero";
case 10:
return "Ten";
case 11:
return "Eleven";
case 12:
return "Twelve";
case 13:
return "Thirteen";
case 14:
return "Forteen";
case 15:
return "Fifteen";
case 16:
return "Sixteen";
case 17:
return "Seventeen";
case 18:
return "Eighteen";
case 19:
return "Ninteen";
case 20:
return "Twenty";
case 30:
return "Thirty";
case 40:
return "Forty";
case 50:
return "Fifty";
case 60:
return "Sixty";
case 70:
return "Seventy";
case 80:
return "Eighty";
case 90:
return "Ninty";
case 100:
return "Hundred";
} //switch return ""; } //getWord
private String cleanNumber( String number ){ String cleanedNumber
= "";
cleanedNumber = number.replace( '.', ' ' ). replaceAll(
" ", "" );
cleanedNumber = cleanedNumber.replace( ',', ' ' ).replaceAll(
" ", "" );
if( cleanedNumber.startsWith( "0" ) ) cleanedNumber =
cleanedNumber.replaceFirst( "0",
"" );
return cleanedNumber; } //cleanNumber
public String convertNumber( String number ){ number =
cleanNumber( number );
double num = 0.0; try{
num = Double.parseDouble( number ); }catch( Exception
e ){
return "Invalid Number Sent to Convert"; } //catch
String returnValue = ""; while( num > 0 ){ number = ""
+ (int)num;
double place = getPlace(number);
if( place == DefinePlace.TENS || place == DefinePlace.TENTHOUSANDS
|| place == DefinePlace.TENMILLIONS || place == DefinePlace.TENBILLIONS
){
//if( place == DefinePlace.TENS || place == DefinePlace.TENTHOUSANDS
|| place ==
//DefinePlace.TENMILLIONS || place == DefinePlace.TENCRORES
){
int subNum = Integer.parseInt( number.charAt(0) + "" +
number.charAt(1) );
if( subNum >= 21 && (subNum%10) != 0 ){ returnValue
+= getWord( Integer.parseInt( "" +
number.charAt(0) ) * 10 ) + " " + getWord( subNum%10
) ; } //if
else{
returnValue += getWord(subNum); }//else
if( place == DefinePlace.TENS ){ num = 0;
}//if
else if( place == DefinePlace.TENTHOUSANDS ){ num -=
subNum * DefinePlace.THOUSANDS; returnValue += " Thousand ";
}//if
else if( place == DefinePlace.TENMILLIONS ){ num -= subNum
* DefinePlace.MILLIONS; returnValue += " Million ";
}//if
else if( place == DefinePlace.TENBILLIONS ){ num -= subNum
* DefinePlace.BILLIONS; returnValue += " Billion ";
//}//if
//else if( place == DefinePlace.TENLAKHS ){ //num -=
subNum * DefinePlace.LAKHS; //returnValue += " Lakhs ";
//}//if
//else if( place == DefinePlace.TENCRORES ){ //num -=
subNum * DefinePlace.CRORES; //returnValue += " Crores ";
}//if }//if else{
int subNum = Integer.parseInt(
"" + number.charAt(0) );
returnValue += getWord( subNum ); if( place == DefinePlace.UNITS
){ num = 0;
}//if
else if( place == DefinePlace.HUNDREDS
){
num -= subNum * DefinePlace.HUNDREDS; returnValue += "
Hundred ";
}//if
else if( place == DefinePlace.THOUSANDS ){ num -= subNum
* DefinePlace.THOUSANDS; returnValue += " Thousand ";
}//if
else if( place == DefinePlace.MILLIONS ){ num -= subNum
* DefinePlace.MILLIONS; returnValue += " Million ";
}//if
else if( place == DefinePlace.BILLIONS ){ num -= subNum
* DefinePlace.BILLIONS; returnValue += " Billion ";
//}//if
//else if( place == DefinePlace.LAKHS ){ //num -= subNum
* DefinePlace.LAKHS; //returnValue += " Lakh ";
//}//if
//else if( place == DefinePlace.CRORES ){ //num -= subNum
* DefinePlace.CRORES; //returnValue += " Crore ";
}//if }//else }//while
return returnValue; }//convert number
public static void main( String args[] ){ Kamikuto1 cv
= new Kamikuto1();
if( args.length >= 1 )
{
for( int i=0; i<args.length; i++ ) System.out.println(
"Given Number : " + args[i] +
"\nConverted: " + cv.convertNumber(args[i]) + "\n\n"
); System.exit(0);
}
System.out.println( "Given Number : 999999999\nConverted:
" + cv.convertNumber("999999999") + "\n\n" );
}//main } //class
class DefinePlace{
public static final double UNITS
= 1;
public static final double TENS = 10 * UNITS; public
static final double HUNDREDS = 10 * TENS;
public static final double THOUSANDS = 10 * HUNDREDS;
public static final double TENTHOUSANDS = 10 * THOUSANDS;
public static final double MILLIONS = 10 * TENTHOUSANDS;
public static final double TENMILLIONS
= 10 * MILLIONS;
public static final double BILLIONS = 10 * TENMILLIONS;
public static final double TENBILLIONS = 10 * BILLIONS;
//public static final double LAKHS = 10 * TENTHOUSANDS;
//public static final double TENLAKHS = 10 * LAKHS;
//public static final double CRORES = 10 * TENLAKHS;
//public static final double TENCRORES = 10 * CRORES;
} //class
Do you have a Java Problem?
Ask It in The Java
Forum
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|