是否可以将字母形式的数字字符串数组转换为数字形式?

问题描述 投票:1回答:3

我对Java还是很陌生,但是对于我想测试的理论有疑问。我想知道是否有一种编写程序的方法,该程序可以将数字字符串数组(以字母1、2、3等写成)转换为数字形式的整数。这实际上可行吗?我没有任何示例代码,因为我尚未对其进行测试。但是,一些答案将不胜感激。谢谢!

java arrays string int theory
3个回答
0
投票

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

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

如果要转换的数组很多,那么就必须在其上循环,并且很显然,解析可能会因为可能的字符串值范围更广而变得更加复杂,例如:“四千三十五”]


0
投票

通常,我通过“配置优先于实现”模式来解决此类问题。我可以和您分享您问题的解决方案。

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

0
投票

您还将在财产档案中需要十,十岁,十二,十三,...,二十,三十,..九十,一百,千,百万..然后,您必须解析书面数字。

““ 12.23万”必须是(2x100 + 12)x 1000 + 3。

使用以上数字,您知道必须停止在100并乘以2。您还知道,当您得到“千”时,其剩余的数必须乘以1000。除非左边还有一百万。

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

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

还请注意,没有发现非法的书面数字,例如“ 1,299.9454”。这将是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
© www.soinside.com 2019 - 2024. All rights reserved.