I want to convert a number in there equivalent word. I write down a code and using Java dictionary for this purpose. My code work well for a three digit number. But when the number is more than 3 digit it failed to produce output. Also it doesn't work if it's one position is zro and tens position is from ten to nineteen.
My code
public class IntegerEnglish {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter the integer");
int input_number=in.nextInt();
int a[]=new int[3];//define array for 3 digit but it should depend upon countdigit. But I cannot specify countdigit here because it is written after this line.
int countdigit=0,i=0;
Dictionary numbers_converter=new Hashtable();//Represent ones position
Dictionary number_place=new Hashtable();//Represent thousand,ten thousand,tem million position
Dictionary number_2nd=new Hashtable();//Represent tens position
Dictionary number_converter1=new Hashtable();//Represent position ten to nineteen
numbers_converter.put(1,"One");
numbers_converter.put(2,"Two");
numbers_converter.put(3,"Three");
numbers_converter.put(4,"Four");
numbers_converter.put(5,"Five");
numbers_converter.put(6,"Six");
numbers_converter.put(7,"Seven");
numbers_converter.put(8,"Eight");
numbers_converter.put(9,"Nine");
number_converter1.put(10,"Ten");
number_converter1.put(11,"Eleven");
number_converter1.put(12,"Twelve");
number_converter1.put(13,"Thirteen");
number_converter1.put(14,"Fourteen ");
number_converter1.put(15,"Fifteen");
number_converter1.put(16,"Sixteen");
number_place.put(3,"Hundred");
number_place.put(4,"Thousand");
number_place.put(7,"Million");
number_place.put(11,"Billion");
number_2nd.put(2,"Twenty");
number_2nd.put(3,"Thirty");
number_2nd.put(4,"Forty");
number_2nd.put(5,"Fifty");
number_2nd.put(6,"Sixty");
number_2nd.put(7,"Seventy");
number_2nd.put(8,"Eighty");
number_2nd.put(9,"Ninty");
while(input_number>0){
a[i] = input_number % 10;
i++;
input_number = input_number / 10;
countdigit = countdigit + 1;//Counter that calculate how many digit are there in this number.
if(countdigit==3||countdigit==4||countdigit==7||countdigit==11) {
System.out.print(numbers_converter.get(a[2]));
System.out.print(number_place.get(countdigit));//Statement for thousand,ten thousand ....position
}
}
System.out.print(number_2nd.get(a[1]));//Like hard code only work when number is 3 digit.
System.out.print(numbers_converter.get(a[0]));//Need some counter which used to change the cell number depend upon number of digit
}
}
Output:Enter the integer
123 OneHundredTwentyThree
So far I saw various example that are available here. No one using Java dictionary. So how could I proceed that make my code useful for any number up to billion?
Aucun commentaire:
Enregistrer un commentaire