title: Recursive expression evaluator using Java
body: <p>I am going to write an expression evaluator which only does addition and subtraction. I have a simple algorithm to do that; but, I have some implementation problems.</p>
  
  <p>I considered an expression as (it is a String)</p>
  
  <pre><code>"(" &lt;expression1&gt; &lt;operator&gt; &lt;expression2&gt; ")"
  </code></pre>
  
  <p>Here is my algorithm</p>
  
  <pre><code>String evaluate( String expression )
  
     if expression is digit
        return expression
  
     else if expression is "(" &lt;expression1&gt; &lt;operator&gt; &lt;expression2&gt; ")"
        cut the brackets out of it
        expression1 = evaluate( &lt;expression1&gt; )
        operator = &lt;operator&gt;
        expression2 = evaluate( &lt;expression2&gt; )
  
     if operator is +
        expression1 + expression2
  
     else if operator is -
        expression1 - expression2 
  </code></pre>
  
  <p>My problem is parsing <code>&lt;expression1&gt;</code>, <code>&lt;operator&gt;</code> and <code>&lt;expression2&gt;</code> from the expression. How can I do that? </p>
  
  <p>Note: I'm not asking for a code. All I need is an idea to do that. </p>
  
  <p>Thank you, </p>
  
  <p>-Ali</p>