实现Number to String程序

时间:2016-11-16 10:13:56

标签: java

我已经编写了一个代码,用于将数字转换为考虑约束1<=number<=1000000000(印度编号系统)的单词。

我在其实施中使用了HashMap

问题是我无法获得某些特殊案例编号的输出。是否有任何替代和有效的方法来实现这一点。

代码

import java.util.HashMap;
import java.util.Scanner;

public class NumberToString {

    public static void main(String[] args) {
        HashMap<Long, String> hm = new HashMap<Long, String>() {
            {
                put(0L, "Zero");
                put(1L, "One");
                put(2L, "Two");
                put(3L, "Three");
                put(4L, "Four");
                put(5L, "Five");
                put(6L, "Six");
                put(7L, "Seven");
                put(8L, "Eight");
                put(9L, "Nine");
                put(10L, "Ten");
                put(11L, "Eleven");
                put(12L, "Twelve");
                put(13L, "Thirteen");
                put(14L, "Fourteen");
                put(15L, "Fifteen");
                put(16L, "Sixteen");
                put(17L, "Seventeen");
                put(18L, "Eighteen");
                put(19L, "Nineteen");
                put(20L, "Twenty");
                put(30L, "Thirty");
                put(40L, "Fourty");
                put(50L, "Fifty");
                put(60L, "Sixty");
                put(70L, "Seventy");
                put(80L, "Eighty");
                put(90L, "Ninety");
            }
        };

        int multiplier = 1;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a number:");
        String numberString = scanner.nextLine();
        try {
            if (Long.parseLong((numberString)) == 0) {
                System.out.println("Enter a number greater than zero");
                System.exit(0);
            }
        } catch (NumberFormatException e) {
            System.out.println(numberString + "is not a number");
            System.exit(0);
        }
        Long[] numberInteger = new Long[numberString.length()];
        for (int i = 0; i < numberInteger.length; i++) {
            numberInteger[i] = (long) (numberString.charAt(i) - '0');
        }

        for (int i = (numberInteger.length - 1); i >= 0; i--) {
            numberInteger[i] = numberInteger[i] * multiplier;
            multiplier = multiplier * 10;
        }
        int temp = 0;
        for (int i = 0; i < numberInteger.length; i++) {
            int counter = 0;
            while (numberInteger[i] != 0 && numberInteger[i] % 10 == 0) {
                counter++;
                numberInteger[i] = numberInteger[i] / 10;
            }
            if (counter == 9)
                System.out.print(hm.get(numberInteger[i]) + " hundred and ");
            else if (counter == 8) {
                if ((Long.parseLong(numberString)) % 10 == 0)
                    System.out.print(hm.get(numberInteger[i] * 10) + " crores ");
                else
                    System.out.print(hm.get(numberInteger[i] * 10) + " ");
            } else if (counter == 7)
                System.out.print(hm.get(numberInteger[i]) + " crores ");
            else if (counter == 6) {
                if ((Long.parseLong(numberString)) % 10 == 0)
                    System.out.print(hm.get(numberInteger[i] * 10) + " lakhs ");
                else
                    System.out.print(hm.get(numberInteger[i] * 10) + " ");
            } else if (counter == 5)
                System.out.print(hm.get(numberInteger[i]) + " lakhs ");
            else if (counter == 4) {
                if ((Long.parseLong(numberString)) % 10 == 0)
                    System.out.print(hm.get(numberInteger[i] * 10) + " thousand");
                else
                    System.out.print(hm.get(numberInteger[i] * 10) + " ");
            } else if (counter == 3)
                System.out.print(hm.get(numberInteger[i]) + " thousand ");
            else if (counter == 2)
                System.out.print(hm.get(numberInteger[i]) + " hundred ");
            else if (counter == 1) {
                if (numberInteger[i] == 1) {
                    temp = 10;
                    continue;
                } else
                    System.out.print(hm.get(numberInteger[i] * 10) + " ");
            } else if (counter == 0) {
                if (temp == 10)
                    System.out.print(hm.get(numberInteger[i] + temp));
                if (numberInteger[i] > 0 && temp != 10)
                    System.out.print(hm.get(numberInteger[i]));
            } else
                System.out.print(hm.get(numberInteger[i]));
        }
        scanner.close();
    }
}

1 个答案:

答案 0 :(得分:2)

here所示,Java中有没有方法为您执行此操作

你可以做的是尝试创建一个生成数字字符串的方法。

因此,此方法将包含表示每个数字所需的所有字符串:

zero - nine, ten, twenty, thirty , hundred, thousand, [m-b-tr-quad..]illion 
eleven, twelve, thirteen and negative

14 - 19 => [4-9] + teen
30 - 90 => [3-9] + ty
10^6 - 10^3n => [m for 10^6, b for 10^9] + illion

您必须删除重叠的t

i.e.它是Eighty而不是Eightty

我刚创建了一个适用于-1000000000 < n < 1000000000的程序。 很抱歉延迟但我想确保它正常工作。

代码

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberConversion {
    public static final int MAX_NUMBER = 999999999;// up to almost 1 billion
    public static final HashMap<Integer, String> numbers;
    public static final HashMap<String, String> patterns;
    public static final String NEGATIVE = "Minus ";
    public static final String ZERO = "Zero";

    static {
        numbers = new HashMap<Integer, String>();
        numbers.put(0, "");
        numbers.put(1, "One");
        numbers.put(2, "Two");
        numbers.put(3, "Three");
        numbers.put(4, "Four");
        numbers.put(5, "Five");
        numbers.put(6, "Six");
        numbers.put(7, "Seven");
        numbers.put(8, "Eight");
        numbers.put(9, "Nine");
        numbers.put(10, "Ten");
        numbers.put(11, "Eleven");
        numbers.put(12, "Twelve");
        numbers.put(13, "Thirteen");
        numbers.put(15, "Fifteen");
        patterns = new HashMap<String, String>();
        patterns.put("1[4-9]", "[]teen ");
        patterns.put("2.", "Twenty ");
        patterns.put("3.", "Thirty ");
        patterns.put("[4-9].", "[]ty ");
        patterns.put("[1-9]..", "[] Hundred ");
        patterns.put("[0-9]{1,3}...", "[] Thousand ");
        patterns.put("[0-9]{1,3}......", "[] Million ");
    }

    public static String getValue(int number) {
        return number != 0 ? getValue(String.valueOf(number)) : "Zero";
    }

    public static String getValue(String number) {
        // Validation
        int parsed = Integer.parseInt(number);
        if (parsed > MAX_NUMBER) {
            throw new IllegalArgumentException("The number: '" + number + "' is too large to be converted.");
        }
        if (parsed < 0) {
            return NEGATIVE + getValue(number.substring(1));
        }
        // If the number exists in the numbers map, return it as is.
        if (numbers.containsKey(parsed)) {
            return numbers.get(parsed);
        }
        StringBuilder result = new StringBuilder();// The returned string
        Iterator<Entry<String, String>> iterator = patterns.entrySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next().getKey();
            Pattern pattern = Pattern.compile(key);
            Matcher matcher = pattern.matcher(number);
            if (matcher.matches()) {
                int index = key.indexOf('.');
                String temp;
                if (index <= 0) {
                    index = key.indexOf('[');
                    temp = number.substring(index);
                } else {
                    int size = key.length() - index;
                    index = number.length() - size;
                    temp = number.substring(0, index);
                }
                String string = patterns.get(key);
                number = number.substring(index);
                result.append(string.replace("[]", getValue(temp)));
            }
        }
        if (number != "") {
            /*
             * In the event of having a number like 121. The output will be 120
             * and the variable number will still hold the 1.
             */
            result.append(getValue(number));
        }
        // Eightten -> Eighteen, Eightty -> Eighty
        return result.toString().replace("tt", "t");
    }

    public static void main(String[] args) {
        System.out.println(getValue(312321));
    }
}

如果您想添加patterns.put("[0-9]{1,3}.........", "[] Billion "); 您必须将每int更改为long