Frage

I am rebuilding a stack calculator so it is recursive. However, I get an error:

stacks2.java:29: illegal start of expression
public calculate(Stack<String> stack) {
^
stacks2.java:29: ';' expected
public calculate(Stack<String> stack) {

How are you supoosed to pass a stack into a method correctly.

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<String> rpnStack = new Stack<String>();
//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<String> stack) {
    // Base case: stack is empty => Error, or finished
    if (!stack.isEmpty())
      // throw new StackUnderflowException("Empty Stack");

    // Base case: stack has 1 element, which is the answer => finished
    if (stack.size(1))
        System.out.printf("Finished, Answer: %f\n",stack.peek());

    // Recursive case: stack more elements on it.
    if (stack.size() > 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.");
            }
        }  
    }
}

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.

War es hilfreich?

Lösung

You need to give a return type to your method. Also, since you are calling it directly from your main method, it should be static. So, change your method declaration to: -

public static void calculate(Stack<String> stack)

Also, you missed the closing brace of main method, after the while loop. You need to put an extra curly brace there.

P.S: - Class name in Java should always be in CamelCase with the first letter of every word should be in UpperCase. Also, class name should always be singular not plurals.

So, stacks2 should really be Stack2, or even better, you can name it MyStack

You have used: -

Float.parseFloat(temp2);// Convert temp2 into a float

without any L-Value. Float.parseFloat returns the primitive float value. You need to store it somewhere, else it's of no use.

So, you should replace the below code: -

Float.parseFloat(temp2);
Float.parseFloat(temp1);
float resultant = temp1 * temp2;

with: -

float resultant = Float.parseFloat(temp1) * Float.parseFloat(temp2);

Andere Tipps

You need to put the end } of the main function. You mistake the end } of the while loop for it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top