Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:48:05+00:00 2026-06-13T09:48:05+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:48:06+00:00Added an answer on June 13, 2026 at 9:48 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I previously had a post on this issue that was resolved. However since rebuilding
I'm currently rebuilding an existing PHP application using Java. I therefore have an existing
I have a HTML5 UI and a Java backend and want to avoid rebuilding
I'm struggling to get my head around what should happen when rebuilding the model
I'm trying to create a generic parsed string cache to prevent rebuilding the same
We're building a multi-language Drupal stack and one of the concerns we have is
rebuilding a mac from scratch. Installed xcode and rvm then trying to install rubies
I am rebuilding an application to comply with MISRA-rules and using QA-C to analyze
I am rebuilding indexes using a script which reorganises or rebuilds indexes according to
I am planning to rebuilding the code of one asp.net project to another asp.net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.