title: Method for evaluating math expressions in Java
body: <p>In one of my projects I want to add a feature where the user can provide in a formula, for example</p>
  
  <pre><code>sin (x + pi)/2 + 1
  </code></pre>
  
  <p>which I use in my Java app</p>
  
  <pre><code>/**
   * The formula provided by the user
   */
  private String formula; // = "sin (x + pi)/2 + 1"
  
  /*
   * Evaluates the formula and computes the result by using the
   * given value for x
   */
  public double calc(double x) {
      Formula f = new Formula(formula);
      f.setVar("x", x);
      return f.calc();
      // or something similar
  }
  </code></pre>
  
  <p>How can I evaluate math expressions?</p>