1

I need to find all numbers in string and do simple arithmetic with them. If count of the symbols between two numbers are even, then operator is '+', if count is odd, then operator is '-'.

Input: 10plus5 - Output: 15; (10 + 5);


Input: 10i5can3do2it6 - Output: 10; (10 - 5 - 3 + 2 + 6);


Input: 10i5can3do2it - Output: 4; (10 - 5 - 3 + 2);

I can find solution only for the first example:

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);
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
martin_tsv
  • 39
  • 1
  • 6

3 Answers3

4

The following solution only works if the input String starts with a number followed by a series of word-number combinations (doesn't need to end with a number). Haven't included the validation for this.

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

Prints 4.

The regex splits between letters and digits as explained here. The rest is pretty self-explanatory.

Robin Topper
  • 2,295
  • 1
  • 17
  • 25
3

As I mention in the comments, you are only calculating the results of the final two numbers.

You need to carry the calculation through.

For example. (Verified all 3 given inputs)

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);
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

The pattern is very simple, perhaps a simple approach is better, for example:

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));
}
David Pérez Cabrera
  • 4,960
  • 2
  • 23
  • 37