Converting Numbers to Words in Java
Step 1: Download the program and save it as Naveen.java
[java]
package pack;
import java.util.ArrayList;
import java.util.List;
public class Naveen {
public static void main(String[] args) {
String[] zerotonineteen={" zero"," one"," two"," three"," four"," five", " six"," seven"," eight"," nine"," ten"," eleven", " twelve", " thirteen", " fourteen", " fifteen",
" sixteen", " seventeen", " eighteen", " nineteen"};
String[] tens ={"",""," twenty"," thirty"," forty"," fifty"," sixty"," seventy"," eighty"," ninety"};
String[] remainings = {" hundred", " thousand", " million", " billion", " trillion"};
String naveen =args[0];
String naveendra = naveen.replaceFirst("0", " ").trim();
Integer input = Integer.parseInt(naveendra);
String[] conver = naveendra.split("");
int lengthdigit = (int)(Math.log10(Integer.parseInt(naveen))+1);
if(input<=19){
System.out.println("you have entered "+zerotonineteen[input]);
}
else if(input>=20 && input<=99){
if(Integer.parseInt(conver[2])==0){
System.out.println("you have entered "+tens[Integer.parseInt(conver[1])]);
}
else{
System.out.println("you have entered "+tens[Integer.parseInt(conver[1])]+zerotonineteen[Integer.parseInt(conver[2])]);
}
}
else if(input>99 && input<=999){
if((Integer.parseInt(conver[2])==0)&&(Integer.parseInt(conver[3])==0)){
System.out.println("you have entered "+zerotonineteen[Integer.parseInt(conver[1])]+remainings[0]);
}
else{
System.out.println("you have entered "+zerotonineteen[Integer.parseInt(conver[1])]+remainings[0]+" and"+tens[Integer.parseInt(conver[2])]+zerotonineteen[Integer.parseInt(conver[3])]);
}
}
else if(input > 999 && input <=9999){
if((Integer.parseInt(conver[2])==0)&&(Integer.parseInt(conver[3])==0)&&(Integer.parseInt(conver[4])==0)){
System.out.println("you have entered "+zerotonineteen[Integer.parseInt(conver[1])]+remainings[1]);
}
else if((Integer.parseInt(conver[3])==0)&&(Integer.parseInt(conver[4])==0)){
System.out.println("you have entered "+zerotonineteen[Integer.parseInt(conver[1])]+remainings[1]+" and"+zerotonineteen[Integer.parseInt(conver[2])]+remainings[0]+tens[Integer.parseInt(conver[3])]);
}
else if((Integer.parseInt(conver[2])==0)&&Integer.parseInt(conver[3]+conver[4])<=19){
System.out.println("you have entered "+zerotonineteen[Integer.parseInt(conver[1])]+remainings[1]+" and"+zerotonineteen[Integer.parseInt(conver[3]+conver[4])]);
}
else if((Integer.parseInt(conver[2])==0)&&Integer.parseInt(conver[3]+conver[4])>19&&Integer.parseInt(conver[3]+conver[4])<=99){
System.out.println("you have entered "+zerotonineteen[Integer.parseInt(conver[1])]+remainings[1]+" and"+tens[Integer.parseInt(conver[3])]+zerotonineteen[Integer.parseInt(conver[4])]);
}
else if((Integer.parseInt(conver[4])==0)){
System.out.println("you have entered "+zerotonineteen[Integer.parseInt(conver[1])]+remainings[1]+zerotonineteen[Integer.parseInt(conver[2])]+remainings[0]+" and"+tens[Integer.parseInt(conver[3])]);
}
else
{
System.out.println("you have entered "+zerotonineteen[Integer.parseInt(conver[1])]+remainings[1]+zerotonineteen[Integer.parseInt(conver[2])]+remainings[0]+" and"+tens[Integer.parseInt(conver[3])]+zerotonineteen[Integer.parseInt(conver[4])]);
}
}
}
}
[/java]
Step 2:
If you are using eclipse then cick
Run as -> Run configuration -> Arguments -> 1234 (to give command line inputs).
If you are running using cmd then
Naveen 1234.
Step 3:
Output:
you have entered one thousand two hundred and thirty four