title: How to find value of prefix expression using a recursive method?
body: <p>I have the following pseudocode and need to write a java method to evaluate a prefix expression:</p>
  
  <p>Algorithm valueOfPrefixExpression(prefixExpression)<br>
  Input: a valid positive-integer arithmetic expression in prefix form<br>
  Return value: the value of the prefix expression</p>
  
  <p>if next token is an integer<br>
      return the integer<br>
  else<br>
      Read an operator, say op<br>
      firstOperand gets valueOfPrefixExpression(remainingExpression)<br>
      secondOperand gets valueOfPrefixExpression(remainingExpression)<br>
      return firstOperand op secondOperand<br>
  endif  </p>
  
  <p>How can I write this method? I tried this and I think it could be right but I am getting a "missing return statement" error so I can't compile. Assume method is only called if args has 1 or more elements. (no empty arrays)</p>
  
  <pre><code>public static int prefixAlgorithm(String[] args) {
      for (int i = 0; i &lt; args.length; i++) {
          if (!args[i].equals("+") &amp;&amp; !args[i].equals("-")
                  &amp;&amp; !args[i].equals("*") &amp;&amp; !args[i].equals("/")) {
              int operand = parseInt(args[i]);
              return operand;
          } else {
              int firstOperand = prefixAlgorithm(Arrays.copyOfRange(args, i, (args.length - 1)));
              int secondOperand = prefixAlgorithm(Arrays.copyOfRange(args, i, (args.length - 1)));
              if (args[i].equals("+")) {
                  return firstOperand + secondOperand;
              } else if (args[i].equals("-")) {
                  return firstOperand - secondOperand;
              } else if (args[i].equals("*")) {
                  return firstOperand * secondOperand;
              } else if (args[i].equals("/")) {
                  return firstOperand / secondOperand;
              }
          }
      }
  }
  </code></pre>