1

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.

2 Answers 2

3

You're using peek() which doesn't actually remove the next character from the input, so it loops on and on. You need nextInt();

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah. peek() is used to see what's next in the input, while still leaving it as the next input.
I just edited it (I'm using double now), but if the doubles are added to the stack pemdas then nextInt can't read the top element. So are you sure you'd use nextInt?
1

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
{
}

4 Comments

great. But the string equality makes sense. You cant compare like an int double or boolean.
Exception in thread "main" java.lang.NumberFormatException: For input string: "*" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222) at java.lang.Double.parseDouble(Double.java:510) at stacks.main(stacks.java:65)
this error occured when you added an operate after the prompt, my friend said if you change it to a char it should work but why?
See latest update. You are processing the * as both an operator and as a number. Need more else's

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.