3
\$\begingroup\$

The number is ranging from 1 to 999999999.

public class ConvertIntegertoEnglish {

    private String getCountSuffix(int value){
        String word = "";
        switch(value){
        case 3:
            word = "Hundred "; return word;
        case 4:
            word = "Thousand "; return word;
        case 5:
            word = "Thousand "; return word;
        case 6:
            word = "Lac "; return word;
        case 7:
            word = "Lacs "; return word;
        case 8:
            word = "Crore "; return word;
        case 9:
            word = "Crores "; return word;
        default:
            return "";
        }

    }

    private String getCountPrefix(String count){
        String word = "";
        int value = Integer.valueOf(count);

        if(value >=20 && value <=29){
            word = "Twenty ";
        }else if(value >=30 && value <=39){
            word = "Thirty ";
        }else if(value >=40 && value <=49){
            word = "Fourty ";
        }else if(value >=50 && value <=59){
            word = "Fifty "; 
        }else if(value >=60 && value <=69){
            word = "Sixty "; 
        }else if(value >=70 && value <=79){
            word = "Seventy ";
        }else if(value >=80 && value <=89){
            word = "Eighty ";
        }else if(value >=90 && value <=99){
            word = "Ninty ";
        }else if(value == 0){
            word = "";
        }else if(value == 1){
            word = "One ";
        }else if(value == 2){
            word = "Two ";
        }else if(value == 3){
            word = "Three ";
        }else if(value == 4){
            word = "Four ";
        }else if(value == 5){
            word = "Five ";
        }else if(value == 6){
            word = "Six ";
        }else if(value == 7){
            word = "Seven ";
        }else if(value == 8){
            word = "Eight ";
        }else if(value == 9){
            word = "Nine ";
        }else if(value == 10){
            word = "Ten ";
        }else if(value == 11){
            word = "Eleven ";
        }else if(value == 12){
            word = "Tweleve ";
        }else if(value == 13){
            word = "Thirteen ";
        }else if(value == 14){
            word = "Fourteen ";
        }else if(value == 15){
            word = "Fifteen ";
        }else if(value == 16){
            word = "Sixteen ";
        }else if(value == 17){
            word = "Seveneteen ";
        }else if(value == 18){
            word = "Eighteen ";
        }else if(value == 19){
            word = "Nineteen ";
        }
        return word;
    }

    public String convertIntoEnglish(int num){
        String number = ""+num;
        int len = number.length();
        String sentence ="";
        String temp ="";
        for (int i=len;i>0;i--){
            // 00 00 00 000
            int n = len - i;
            if((i%2)>0 && i>3 ){
                temp = number.substring(n,(n + 2));
                int v = Integer.parseInt(temp);
                if(v < 10) continue;
                sentence = sentence + getCountPrefix(temp);
                if(v > 19){
                    temp = temp.substring(1);
                    sentence = sentence + getCountPrefix(temp);
                }
                sentence = sentence + getCountSuffix(i);
                i--;
            }else if((i%2)==0 && i>3){
                temp = number.substring(n,(n + 1));
                sentence = sentence + getCountPrefix(temp);
                sentence = sentence + getCountSuffix(i);
            }else if(i == 3){
                temp = number.substring(n,(n + 1));
                if(temp.startsWith("0")) continue;
                sentence = sentence + getCountPrefix(temp);
                sentence = sentence + getCountSuffix(3);
            }else{
                if(i == 2)
                    temp = number.substring(len-2);
                else
                    temp = number.substring(len-1);
                int v = Integer.parseInt(temp);
                sentence = sentence + getCountPrefix(temp);
                if(v > 19){
                    temp = temp.substring(1);
                    sentence = sentence + getCountPrefix(temp);
                }
                i--;
            }
        }
        return sentence;
    }

    public static void main(String[] str){
        System.out.println(new ConvertIntegertoEnglish().convertIntoEnglish(23451147));
    }

}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to Code Review! I improve the quality of your posts and increase the chance of a high-value answer, please take a look at our How to ask a good question on meta. \$\endgroup\$
    – Mast
    Commented May 29, 2016 at 14:47
  • \$\begingroup\$ Is this really a number to English conversion? "Crore", "Lac" or the like don't mean anything in English. \$\endgroup\$
    – Tunaki
    Commented May 29, 2016 at 18:07
  • 2
    \$\begingroup\$ @Tunaki Those are large numbers in Indian English. \$\endgroup\$ Commented May 29, 2016 at 18:10

1 Answer 1

4
\$\begingroup\$

There is no point in instantiating the class. All the methods could be static (and the constructor private) to provide the same functionality.

The goal is to map some String values to certain int values. A HashMap provides exactly that functionality.

import java.util.HashMap;

public final class Converter
{
    private static final HashMap<Integer, String> suffixes;
    private static final HashMap<Integer, String> prefixes;

    static
    {
        suffixes = new HashMap<Integer, String>();
        suffixes.put(3, "Hundred ");
        suffixes.put(4, "Thousand ");
        suffixes.put(5, "Thousand ");
        suffixes.put(6, "Lac ");
        suffixes.put(7, "Lacs ");
        suffixes.put(8, "Crore ");
        suffixes.put(9, "Crores ");

        prefixes = new HashMap<Integer, String>();
        prefixes.put(0, "");
        prefixes.put(1, "One ");
        prefixes.put(2, "Two ");
        prefixes.put(3, "Three ");
        prefixes.put(4, "Four ");
        prefixes.put(5, "Five ");
        prefixes.put(6, "Six ");
        prefixes.put(7, "Seven ");
        prefixes.put(8, "Eight ");
        prefixes.put(9, "Nine ");
        prefixes.put(10, "Ten ");
        prefixes.put(11, "Eleven ");
        prefixes.put(12, "Tweleve ");
        prefixes.put(13, "Thirteen ");
        prefixes.put(14, "Fourteen ");
        prefixes.put(15, "Fifteen ");
        prefixes.put(16, "Sixteen ");
        prefixes.put(17, "Seveneteen ");
        prefixes.put(18, "Eighteen ");
        prefixes.put(19, "Nineteen ");

        // decades
        prefixes.put(20, "Twenty ");
        prefixes.put(30, "Thirty ");
        prefixes.put(40, "Fourty ");
        prefixes.put(50, "Fifty ");
        prefixes.put(60, "Sixty ");
        prefixes.put(70, "Seventy ");
        prefixes.put(80, "Eighty ");
        prefixes.put(90, "Ninty ");
    }

There's only one entry for each decade, which means some functionality is required to round values to the lower bound of the possible range. Alternatively, you could add all values of the range as entries to the HashMap. The methods to access both maps could look like this:

private static String getCountSuffix(int value)
{
    return suffixes.containsKey(value) ? suffixes.get(value) : "";
}

private static String getCountPrefix(String count)
{
    int value = Integer.valueOf(count);

    if (prefixes.containsKey(value))
    {
        return prefixes.get(value);
    }

    // to decade
    value = (int)Math.floor(value/10)*10;

    if (prefixes.containsKey(value))
    {
        return prefixes.get(value);
    }

    return "";
}

Having the mapping in a data structure like that makes it a lot easier to transition to non-static code if need be. If you want to create several converters for different languages, you could create them by exchanging the used maps.

The method convertIntoEnglish that does the conversion is a pain to read, so much in fact that I exclude it from this review. As a general advice, iInstead of concatenating to a String with +: sentence = sentence + getCountPrefix(temp);, use StringBuilder.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.