如何从字符串中提取数字并计算它们之间的元素?

时间:2017-07-27 07:54:28

标签: java

我需要在字符串中找到所有数字并用它们进行简单的算术运算。如果两个数字之间的符号数是偶数,那么运算符是&#39; <&#39; ,如果count是奇数,则运算符&#39; - &#39 ; 即可。

  

输入: 10plus5 - 输出: 15; (10 + 5);

  

输入: 10i5can3do2it6 - 输出: 10; (10 - 5 - 3 + 2 + 6);

  

输入: 10i5can3do2it - 输出: 4; (10 - 5 - 3 + 2);

我只能为第一个例子找到解决方案:

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String input = br.readLine();
    int result = 0;
    int count = 0;
     Pattern pat = Pattern.compile("([\\d]+)([\\D]+)([0-9]+)");

     Matcher match = pat.matcher(input);

     while(match.find()){
        char[] array = match.group(2).toCharArray();
         for (int i = 0; i < array.length; i++) {
             int firstNumber = Integer.parseInt(match.group(1));
             int secondNumber = Integer.parseInt(match.group(3));
             count++;
            if(count % 2 == 0){
                result = firstNumber + secondNumber ;
            }else{
                result = firstNumber - secondNumber;
            }
        }

     }
     System.out.println(result);
}

3 个答案:

答案 0 :(得分:4)

以下解决方案仅适用于input字符串以数字开头后跟一系列字数组合(不需要以数字结尾)的情况。尚未包含此验证。

String input = "10i5can3do2it";     
String[] parts = input.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");

int result = Integer.valueOf(parts[0]);

for (int i = 2; i < parts.length; i+=2){
    if (parts[i-1].length() % 2 == 1) {
        result -= Integer.valueOf(parts[i]);
    } else {
        result += Integer.valueOf(parts[i]);
    }
}

System.out.println(result);

打印4

正如在here所解释的那样,正则表达式在字母和数字之间分割。其余的都是不言自明的。

答案 1 :(得分:3)

正如我在评论中提到的那样,你只计算最后两个数字的结果。

您需要进行计算。

例如。 (已验证所有3个输入)

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = br.readLine();

    Pattern pat = Pattern.compile("([\\d]+|)([\\D]+)([0-9]+)");
    Matcher match = pat.matcher(input);

    int result = 0;

    while (match.find()) {
        // Pre-capture the groups
        String[] matches = new String[3];
        for (int i = 0; i < 3; i++) {
            matches[i] = match.group(i+1);
        }

        // Handle first number, if exists, else 0
        int firstNumber = 0;
        try {
            firstNumber = Integer.parseInt(matches[0]);
        } catch (NumberFormatException e) {}
        result+=firstNumber;

        // if second number doesn't exist 
        if (matches[2] == null) {
            break;
        }

        // when second number exists, do the logic
        int secondNumber = Integer.parseInt(matches[2]);
        if (matches[1].length() % 2 == 0) {
            result += secondNumber;
        } else {
            result -= secondNumber;
        }
    }
    System.out.println(result);
}

答案 2 :(得分:1)

模式非常简单,也许一个简单的方法更好,例如:

private static int compute(String input, int index) {
    int result = 0;
    int i = index;
    while (i < input.length() && !Character.isDigit(input.charAt(i))) {
        i++;
    }
    if (i < input.length()) {
        int j = i;
        int value = 0;
        while (j < input.length() && Character.isDigit(input.charAt(j))) {
            value = value * 10 + (input.charAt(j) - '0');
            j++;
        }
        if (j < input.length() ){
            result = compute(input, j);
        }
        if ((index - i) % 2  == 0){
            result += value;
        } else {
            result -= value;
        }
    }
    return result;
}

public static void main(String[] args) throws IOException {
    String input = "10i5can3do2it6";
    System.out.println(compute(input, 0));
}