title: How to pass a stack into a method
body: <p>I am rebuilding a stack calculator so it is recursive.  However, I get an error:</p>
  
  <pre><code>stacks2.java:29: illegal start of expression
  public calculate(Stack&lt;String&gt; stack) {
  ^
  stacks2.java:29: ';' expected
  public calculate(Stack&lt;String&gt; stack) {
  </code></pre>
  
  <p>How are you supoosed to pass a stack into a method correctly.</p>
  
  <pre><code>import java.util.*;
  
  public class stacks2 {
  
  public static void main (String []args){
  System.out.printf("Enter a math equation in reverse polish notation:\n");
  
  //Create stack of Strings
  Stack&lt;String&gt; rpnStack = new Stack&lt;String&gt;();
  //Create Scanner 
  Scanner input = new Scanner(System.in);
  //String in = input.next();
  
  while(input != null) {
      String in = input.next();
          // Tokenize string based on spaces.
          StringTokenizer st = new StringTokenizer(in, " ", false);
              while (st.hasMoreTokens()) {
               rpnStack.push(st.nextToken());
           }
      //Send stack to Calculation Method
      calculate(rpnStack);
       }
  }
  
  public static void calculate(Stack&lt;String&gt; stack) {
      // Base case: stack is empty =&gt; Error, or finished
      if (!stack.isEmpty())
        // throw new StackUnderflowException("Empty Stack");
  
      // Base case: stack has 1 element, which is the answer =&gt; finished
      if (stack.size(1))
          System.out.printf("Finished, Answer: %f\n",stack.peek());
  
      // Recursive case: stack more elements on it.
      if (stack.size() &gt; 1){
          String temp1 = stack.peek();
          stack.pop();
          String temp2 = stack.peek();
          stack.pop();
          String temp3 = stack.peek();
          stack.pop();
  
  
              if (temp3.equals("+")){
              float resultant = Float.parseFloat(temp1) + Float.parseFloat(temp2);
              stack.push(String.valueOf(resultant));
              //System.out.println(resultant);
              calculate(stack);
              }
  
              if (temp3.equals("-")){
              float resultant = Float.parseFloat(temp1) - Float.parseFloat(temp2);
              stack.push(String.valueOf(resultant)); 
              //System.out.println(resultant);
              calculate(stack);
              }
  
              else if (temp3.equals("*")){
              float resultant = Float.parseFloat(temp1) * Float.parseFloat(temp2);
              stack.push(String.valueOf(resultant)); 
              //System.out.println(resultant);
              calculate(stack);
              }
  
              else if (temp3.equals("/")){
              float resultant = Float.parseFloat(temp1) / Float.parseFloat(temp2);
              stack.push(String.valueOf(resultant)); 
              //System.out.println(resultant);
              calculate(stack);
              }
  
              else{
              System.out.printf("Something severely has gone wrong.");
              }
          }  
      }
  }
  </code></pre>
  
  <p>Brief description of code: I make a stack, fill it with user input via string tokenizer.  Then I want to past it to my calculate method. It should \should ask the user repeatedly for an expression in RPN and if the expression is valid calculate the result, otherwise announce the error.  To do this I believe i have to calculate and if the RPN is malformed then stop during the calculation. I top the first three strings.  The temp three should always be an operator.  If it isn't then the RPN isn't formatted correctly. If the code is empty then the program throws an underflow exception. When there is 1 element I return it for the answer. I believe my if statements are correct. So yeah I want to see if it works, however that passing the stack in is kinda hindering me right now.</p>