title: Java String Calculator
body: <p>I'm trying to make a simple calculator in Java which takes input in the form of a string and does a simple '+' and '-' operation.</p>
  
  <p>Single digit inputs work but my problem is when i try to implement this for double digit</p>
  
  <p>input string is: 5+20+5+11 <br>
  list 1  = [5, 20, 2, 0, 5, 11, 1]<br>
  list 2  = [+, +, +]<br>
  Answer:27</p>
  
  <p>I need to find a way where after storing [5] in list1 how i can add [5,20] instead of [5,20,2,0] which the current code is doing.</p>
  
  <pre class="lang-java prettyprint-override"><code>public int calC(String input) {
          int len = input.length();
          ArrayList list1 = new ArrayList();
          ArrayList list2 = new ArrayList();
          for (int i = 0; i &lt; len; i++) {
              if ((input.charAt(i) != '+') &amp;&amp; (input.charAt(i) != '-')) {
                  // check if the number is double-digit
                  if ((i + 1 &lt;= len - 1)) {
                      if ((input.charAt(i + 1) != '+')&amp;&amp; (input.charAt(i + 1) != '-')) {
                          String temp = "";
                          temp = temp + input.charAt(i) + input.charAt(i + 1);
                          int tempToInt = Integer.parseInt(temp);
                          // adding the double digit number
                          list1.add(tempToInt);
                      }
                      // add single digit number
                      list1.add(input.charAt(i) - '0');
                  }
              } else {
                  // adding the symbols
                  list2.add(input.charAt(i));
              }
          }
          int result = 0;
          result = result + (int) list1.get(0);
          for (int t = 0; t &lt; list2.size(); t++) {
              char oper = (char) list2.get(t);
              if (oper == '+') {
                  result = result + (int) list1.get(t + 1);
  
              } else if (oper == '-') {
                  result = result - (int) list1.get(t + 1);
              }
          }
          return result;
      }
  </code></pre>
  
  <p>Edit: working version <br>
  @Ker p pag thanks for the updated methods<br>
  input string is: 5+20+5+11<br>
  [5, 20, 5, 11]<br>
  [+, +, +]<br>
  Answer:41<br>
  I'll need to try to implement this with stack as suggested but the current version works</p>
  
  <pre><code>static boolean isDigit(char check) {
      if (Character.isDigit(check)) {
          return true;
      }
      return false;
  }
  public static int calC(String input) {
  
      int len = input.length();
      ArrayList list1 = new ArrayList();
      ArrayList list2 = new ArrayList();
  
      for (int i = 0; i &lt; len; i++) {
          if ((i + 1 &lt;= len - 1)) {
              if (isDigit(input.charAt(i)) &amp;&amp; isDigit(input.charAt(i + 1))) {
                  String temp = input.charAt(i) + "" + input.charAt(i + 1);
                  int toInt = Integer.parseInt(temp);
                  list1.add(toInt);
                  i = i+1;
              } else if (isDigit(input.charAt(i))) {
                  list1.add(input.charAt(i)- '0');
              } else {
                  list2.add(input.charAt(i));
              }
  
          }
      }
  
      int result = 0;
      result = result + (int) list1.get(0);
      for (int t = 0; t &lt; list2.size(); t++) {
          char oper = (char) list2.get(t);
          if (oper == '+') {
              result = result + (int) list1.get(t + 1);
  
          } else if (oper == '-') {
              result = result - (int) list1.get(t + 1);
          }
      }
      return result;
  }
  </code></pre>