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

  • SEARCH
  • Home
  • 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 8741835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:23:13+00:00 2026-06-13T11:23:13+00:00

I am learning stacks right now, I my code compiles find. When I run

  • 0

I am learning stacks right now, I my code compiles find. When I run it, code will not print my debug println’s and the error

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.Vector.ensureCapacityHelper(Vector.java:226)
at java.util.Vector.addElement(Vector.java:573)
at java.util.Stack.push(Stack.java:50)
at stacks.main(stacks.java:56)

is displayed.

My code looks like:

import ch03.stacks.*;

import java.util.*;

public class stacks {

public static void main (String []args){

    System.out.printf("Enter a math equation in reverse polish notation:\n");

    Stack<Double> pemdas = new Stack<Double>();

    Scanner input = new Scanner(System.in);
    String in = input.next();

    double temp1, temp2, resultant = 0;


    while(input.hasNext()){
        if(in == "+"){
        temp1 = pemdas.peek();  
        pemdas.pop();
        temp2 = pemdas.peek();
        pemdas.pop();
        resultant = temp1 + temp2;
        pemdas.push(resultant);
        System.out.println(resultant);
        }

        if(in == "-"){
        temp1 = pemdas.peek();  
        pemdas.pop();
        temp2 = pemdas.peek();
        pemdas.pop();
        resultant = temp1 - temp2;
        pemdas.push(resultant);
        System.out.println(resultant);
        }

        if(in == "*"){
        temp1 = pemdas.peek();  
        pemdas.pop();
        temp2 = pemdas.peek();
        pemdas.pop();
        resultant = temp1 * temp2;
        pemdas.push(resultant);
        System.out.println(resultant);
        }

        if(in == "/"){
        temp1 = pemdas.peek();  
        pemdas.pop();
        temp2 = pemdas.peek();
        pemdas.pop();
        resultant = temp1 / temp2;  
        pemdas.push(resultant);
        System.out.println(resultant);
        }

        else
        pemdas.push(Double.parseDouble(in));
        System.out.println(resultant);

        }



    System.out.println("Answer:"+ resultant);
    }
}

So i first read in the the string of integers in Reverse Polish Notation and then if it is not an operatand, then i pop it on to my stack. At least that’s what I think it is doing. Any help is greatly appreciated.

  • 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-13T11:23:14+00:00Added an answer on June 13, 2026 at 11:23 am

    You are using Scanner hasNext/next incorrectly. You should always precede each next() with a hasNext().

    In your code, you are calling next() in front of the while loop. Then you are using the return value of hasNext() to terminate the the while loop. But … you are never calling ‘next()’ within the while loop. Therefore hasNext() always returns true and you are in an infinite loop. This – combined with the peek issue described earlier – may be growing your stack until you run out of memory.

    The fix is simple. Move next() right after hasNext() within the while loop

    Scanner input = new Scanner(System.in);
    double temp1, temp2, resultant = 0;
    
    while(input.hasNext()) {
        String in = input.next();
    

    Unfortunately the program still does not work because you are not doing string compares correctly. Replace lines like:

    if (in == "+")
    

    with

    if (in.equals("+"))
    

    Hopefully this was just a typo. If you don’t understand why using == for string compares is a problem, you will need to review Java equality.

    Finally, there is a problem with your if logic. Hint: to handle mutually exclusive cases, use code like:

    if () 
    {
    }
    else if ()
    {
    }
    else if ()
    {
    }
    else
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im currently learning stacks in java and have a quick question. what will the
I am learning java and right now i am stuck at memory allocation of
Hello Stack Overflow contributers, I'm a novice programmer learning Python right now, and I
I'm learning about threads right now and am confused as to what's stored in
Learning some VBA. So far, I've constructed this piece of code which should allow
learning the basics of Python, now ready for a bigger challange... it didn't take
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I am learning Python I don't get one thing. Consider this code: class Stack:
I have this learning code I wrote for the morris seq in f# that
So I have been learning assembly and came to the topic of stack, storing

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.