繁体   English   中英

有没有办法将字母形式的数字字符串数组转换为数字形式?

[英]Is there a way to convert a string array of numbers in alphabetic form, into their numeric form?

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

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

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); 

如果您有一系列要转换的内容,那么您必须对其进行循环,显然您的解析会因可能的字符串值范围更广而变得更加复杂,例如:“四千三十五”

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

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

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

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

有了上面的数字,你知道你必须停在一百并乘以二。 你也知道当你得到“千”时,它剩下的任何东西都必须乘以 1000。除非左边还有一百万。

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

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

另请注意,没有检测到非法写入的数字,例如“一 299954”。 这将是 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM