0

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?

Solution link

Encipher
  • 1,370
  • 1
  • 14
  • 31
  • 1
    Not pivotal to the question but you're using raw types, which is a bad idea. Aside from that the max limit for int is `2,147,483,647`, so if you plan to be storing numbers in the billions you'll have to use a different data type – GBlodgett Dec 19 '18 at 02:49
  • 1
    The reason you couldn't find anyone using `Dictionary` is probably that the `Dictionary` class has been obsolete for many years, as explained in its Javadoc. – Dawood ibn Kareem Dec 19 '18 at 03:54
  • Also, I'd be inclined to treat zero as a special case, and write a recursive method that deals with 1 up to 999,999,999,999. – Dawood ibn Kareem Dec 19 '18 at 03:55
  • @GBlodgett Thanks for this reply. As per your suggestion I think it is better to use double. – Encipher Dec 19 '18 at 05:36
  • @DawoodibnKareem Ok. Actually I had take a tutorial of python where they use `Dictionary`. Then I search it for java and get some suggestion about it. Then I use it in my code. If `Dictionary` is obsolete then what data structure they introduce to omit `Dictionary`? – Encipher Dec 19 '18 at 05:39
  • Go and read the Javadoc for Dictionary. It tells you clearly what to use, and how. – Dawood ibn Kareem Dec 19 '18 at 05:40
  • @DawoodibnKareem Thank you. I found out from Java Doc **This class is obsolete. New implementations should implement the Map interface, rather than extending this class.** But dictionary is not only the flaw of this code there are some logical flaw. – Encipher Dec 19 '18 at 05:45
  • Learning how to debug code is an important part of learning to program. I suggest that you read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for some tips. You might already know some of these. The review is always good, though. – Code-Apprentice Dec 19 '18 at 23:27

0 Answers0