title: Arithmetic calculation for BODMAS in Java
body: <p>I have written this code for Bodmas, but getting some error in this. If I do 3-5+9, it will result in 3.04.0.</p>
  
  <p>It just start concatenating, though it works for all other operations like *, / and -, please help.</p>
  
  <pre><code>public static String calculation(BODMASCalculation bodmas, String result) {
      while (bodmas.hasMatch()) {
          double value, leftOfOperator = bodmas.getLeft();
          char op = bodmas.getOperator();
          double rightOfOprator = bodmas.getRight();
  
          switch (op) {
          case '/':
              if(rightOfOprator == 0) //Divide by 0 generates Infinity
                  value = 0;
              else
                  value = leftOfOperator / rightOfOprator;
              break;
          case '*':
              value = leftOfOperator * rightOfOprator;
              break;
          case '+':
              value = leftOfOperator + rightOfOprator;
              break;
          case '-':
              value = leftOfOperator - rightOfOprator;
              break;
          default:
              throw new IllegalArgumentException("Unknown operator.");
          }           
          result = result.substring(0, bodmas.getStart()) + value + result.substring(bodmas.getEnd());
          bodmas = new BODMASCalculation(result);
      }
      return result;
  }
  </code></pre>
  
  <p>Another function is:-</p>
  
  <pre><code>public boolean getMatchFor(String text, char operator) {
      String regex = "(-?[\\d\\.]+)(\\x)(-?[\\d\\.]+)";
      java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(regex.replace('x', operator)).matcher(text);
      if (matcher.find()) {
          this.leftOfOperator = Double.parseDouble(matcher.group(1));
          this.op = matcher.group(2).charAt(0);
          this.rightOfOprator = Double.parseDouble(matcher.group(3));
          this.start = matcher.start();
          this.end = matcher.end();
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>I have a solution by adding</p>
  
  <pre><code>String sss = null;
          if(op == '+' &amp;&amp; !Str.isBlank(result.substring(0, bodmas.getStart())) &amp;&amp; value &gt;= 0)
              sss = "+";
          else
              sss = "";
          result = result.substring(0, bodmas.getStart()) + sss + value   + result.substring(bodmas.getEnd());
  </code></pre>
  
  <p>But don't want to do that, I want this to work for all the operators.</p>