【问题标题】:Is there a way to convert a string array of numbers in alphabetic form, into their numeric form?有没有办法将字母形式的数字字符串数组转换为数字形式?
【发布时间】:2020-04-28 16:51:30
【问题描述】:

我还是 Java 新手,但我对想要测试的理论有疑问。我想知道是否有一种方法可以编写一个程序,将一个数字字符串数组(用一、二、三等字母写出)转换为整数形式的数字形式。这真的可能吗?我没有任何示例代码,因为我还没有对此进行测试。但一些答案将不胜感激。谢谢!

【问题讨论】:

    标签: java arrays string int theory


    【解决方案1】:

    您的财产文件中还需要十、十一、十二、十三、...、二十、三十、.. 九十、一百、千、百万..。然后你必须解析写出来的数字。

    “二十一万二千零三”必须是 (2x100 + 12) x 1000 + 3。

    通过上面的数字,您知道您必须在百处停止并乘以二。你也知道,当你得到“千”时,剩下的必须乘以 1000。除非左边的某个地方也有一百万。

    我尝试了一个从右端开始的示例,即最不重要的数字。我记下键号 100 和 1000 并进行递归计算。我没有包含属性文件,而是使用了地图。如果有一个单词的映射,它将被添加到列表中,除非它是“and”。如果没有单词的映射,则程序会出现 NullPointerException。

    请注意,此代码不应在任何生产环境中使用。它不是解析器,也不会考虑语言的极端情况。

    另请注意,没有检测到非法书写数字,例如“一二九十九五四”。这将是 3000 + 18000 + 9

    public class TextToNumber{
      public static void main(String[] args){
        String s;
        TextToNumber textToNumber = new TextToNumber();
    
        s = "four";
        System.out.println(s);
        System.out.println(textToNumber.translateNumber(s));
    
        System.out.println("----");
        s = "one hundred";
        System.out.println(s);
        System.out.println(textToNumber.translateNumber(s));
    
        System.out.println("----");
        s = "five thousand";
        System.out.println(s);
        System.out.println(textToNumber.translateNumber(s));
    
        System.out.println("----");
        s = "twelve thousand two hundred fifty six";
        System.out.println(s);
        System.out.println(textToNumber.translateNumber(s));
    
        System.out.println("----");
        s = "two hundred and twelve thousand and three";
        System.out.println(s);
        System.out.println(textToNumber.translateNumber(s));
    
        System.out.println("----");
        s = "one two thousand ninety ninety hundred five four";
        System.out.println(s);
        System.out.println(textToNumber.translateNumber(s));
      }
    
      Map<String, Integer> map = new HashMap<>();
      public TextToNumber() {
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);
        map.put("four", 4);
        map.put("five", 5);
        map.put("six", 6);
        map.put("seven", 7);
        map.put("eight", 8);
        map.put("nine", 9);
        map.put("ten", 10);
        map.put("eleven", 11);
        map.put("twelve", 12);
        map.put("thirteen", 13);
        map.put("fourteen", 14);
        map.put("fifteen", 15);
        map.put("sixteen", 16);
        map.put("seventeen", 17);
        map.put("eighteen", 18);
        map.put("nineteen", 19);
        map.put("twenty", 20);
        map.put("thirty", 30);
        map.put("fourty", 40);
        map.put("fifty", 50);
        map.put("sixty", 60);
        map.put("seventy", 70);
        map.put("eighty", 80);
        map.put("ninety", 90);
        map.put("hundred", 100);
        map.put("thousand", 1000);
      }
    
      public int translateNumber(String text){
        List<Integer> numbers = new ArrayList<>(10);
    
        if (text == null) throw new IllegalArgumentException("Number cannot be empty");
    
        // borrowed from https://docs.oracle.com/javase/tutorial/i18n/text/word.html
        BreakIterator wordIterator = BreakIterator.getWordInstance();
        wordIterator.setText(text);
    
        int start = wordIterator.first();
        int end = wordIterator.next();
    
        while (end != wordIterator.DONE) {
            String word = text.substring(start,end);
            if (Character.isLetterOrDigit(word.charAt(0))) {
                if (!("and".equals(word))) numbers.add(map.get(word));
            }
            start = end;
            end = wordIterator.next();
        }
    
        System.out.println(numbers);
        return recursiveCalculation(numbers);
      }
    
      private int recursiveCalculation(List<Integer> list){
        int result = 0;
        int index1000 = list.lastIndexOf(Integer.valueOf(1000));
        int index100  = list.lastIndexOf(Integer.valueOf(100));
    
        // add all numbers that's to the right of the hundred marker
        //  (or 1000 marker in case there's no hundred marker)
        result += add(list, Math.max(index100+1,Math.max(index1000+1,0)), list.size());
    
        if (index100 != -1 && index100 > index1000) {
          result += 100 * recursiveCalculation(list.subList(index1000+1,index100));
        }
    
        if (index1000 != -1) {
          result += 1000 * recursiveCalculation(list.subList(0,index1000));
        }
    
        return result;
      }
    
      private int add(List<Integer> list, int begin, int end) {
        int sum = 0;
        for (int i = begin; i < end; i++) {
          sum += list.get(i);
        }
        return sum;
      }
    }
    

    结果如下:

    C:\..snip..>java -cp compiled/default TextToNumber
    four
    [4]
    4
    ----
    one hundred
    [1, 100]
    100
    ----
    five thousand
    [5, 1000]
    5000
    ----
    twelve thousand two hundred fifty six
    [12, 1000, 2, 100, 50, 6]
    12256
    ----
    two hundred and twelve thousand and three
    [2, 100, 12, 1000, 3]
    212003
    ----
    one two thousand ninety ninety hundred five four
    [1, 2, 1000, 90, 90, 100, 5, 4]
    21009
    

    【讨论】:

      【解决方案2】:

      一般来说,我通过“配置优于实现”模式来解决这种问题。 我可以与您分享您的问题的解决方案吗?

      Java 为我们提供了在属性文件中存储大量键/值的可能性,并可以根据需要多次请求映射。 所以我会存储这个文件:string2int.properties:

      zero:0
      one:1
      two:2 
      three:3
      four:4
      five:5
      six:6
      seven:7
      eight:8
      nine:9
      

      这是 .properties 文件的格式

      然后我可以通过 Properties 对象以这种方式访问​​键的值:

          Properties properties = new Properties(); 
          try { 
              FileInputStream input = new FileInputStream("string2int.properties");
              properties.load(input);
              System.out.println("one is : " + properties.getProperty("one"));
          }
          catch(Exception e) {
              e.printStackTrace();
          }
      

      输出:

      one is : 1
      

      【讨论】:

      • 就目前而言还不错,我也会这样做,但是“九百七十五”不太适合这种方法:-) OP 并不清楚他是否在说话关于数字(所以 975 是一个数字)或数字(所以 975 是三个数字)。
      【解决方案3】:

      您可能希望创建一个映射来将字符串值查找为整数。

      Map<String, Integer> map = new HashMap<>();
      map.put("one", 1);
      map.put("two", 2);
      map.put("three", 3);
      map.put("four", 4);
      

      等

      然后你会解析你的字符串并在地图中查找它

      String text = "four"
      Integer value = map.get(text); 
      

      如果你有一个数组要转换,那么你必须循环它,很明显你的解析会变得更加复杂,因为可能的字符串值范围更广,例如:“四千三十五”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-11
        • 1970-01-01
        • 1970-01-01
        • 2015-12-31
        • 2022-01-23
        相关资源
        最近更新 更多