EmptyStackException error with Stack in Java

13,409

Solution 1

There's a few issues I can see:

  1. eval.substring(i,i); will return an empty string every time. You want eval.substring(i,i + 1);, or even better, eval.charAt(i);.

  2. You'll want to put the returned substring/charAt character in it's own variable within your for loop. Currently it is overriding the eval string after the first iteration.

  3. if (eval.equals(alphabet.substring(0, 52))) doesn't do what you seem to think it does judging by your comment. If you want to check if a string contains another string (or even just a single character), use the methods: String#contains or String#indexOf.


Here's a simple corrected snippet:

    String alphabet = "abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String eval = "blah";
    Stack<Character> chars = new Stack<Character>();

    for(char c : eval.toCharArray()) {
        if(alphabet.indexOf(c) != -1) {
            chars.push(c);
            System.out.println(chars.pop());
        }
    }

Solution 2

The only way that you are getting this error is if:

for (int i = 0; i < eval.length(); i++)
   {
       eval = eval.substring(i,i);

       if (eval.equals(alphabet.substring(0, 52)))
     {
           variable.push(eval);


     }
 System.out.println(variable.pop());
   } 

If you have the System.out.println(variable.pop()); outside of the if condition.

pop

public Object pop()

Removes the object at the top of this stack and returns that object as the value of this function.

Returns: The object at the top of this stack (the last item of the Vector object). Throws:

EmptyStackException - if this stack is empty.

In the code that you posted this is not possible since you have the same number of push and pop, and the push operation comes first than pop.

The code you have put:

enter image description here

the error you are getting:

enter image description here

Solution 3

You need to check that each letter in eval is a letter before you push it onto the stack. After you go through all characters in eval, pop everything off the stack and print it out.

for(int i = 0; i < eval.length(); i++) {

    if(alphabet.contains(eval.charAt(i))) {

        variable.push(eval.charAt(i));
    }
}

while(!variable.isEmpty()) {

    System.out.println(variable.pop());
}

Solution 4

This should resolve your issue: You should only be popping after you push. Thus, they should both be inside the if-statement.

    for (int i = 0; i < eval.length(); i++)
    {
       if (alphabet.contains(eval.substring(i,i+1))
       {
           variable.push(eval.substring(i,i+1));
           System.out.println(variable.pop());
       }
     } 

Solution 5

You want to check whether the characters in a string read from input are letters, and put them in a stac You should consider using the Character class which let you perform all sort of tests on characters, without having to worry about encoding. The method that might be useful in your case is isLetter, which check if the character is a letter.

As for the stack issue, your code actually push a value on top of the stack, and then immediately pop it of, which explains why your stack is empty at the end of the loop. You should use the peek method instead.

Share:
13,409
iii
Author by

iii

Updated on July 14, 2022

Comments

  • iii
    iii over 1 year

    What I'm trying to do is to get the eval variable to have each of its letters put into a stack and then printed out. I'm getting an EmptyStackException error(assuming that means that there isn't anything in the stack). What I don't understand is that I thought that the eval string was put into the variable stack. Why is it empty?

    public static void main(String[] args)
    {
    
       Stack<String> variable = new Stack<String>();
    
       String eval = StdIn.readString();
       String alphabet = "abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    
       for (int i = 0; i < eval.length(); i++)
       {
           eval = eval.substring(i,i);
    
           if (eval.equals(alphabet.substring(0, 52)))// checks if eval is equal to any letter of alphabet
         {
               variable.push(eval);
    
           System.out.println(variable.pop());
         }
       } 
      }
    }
    

    Im using eclipse


    Sample Runs:

    input: hello
    Exception in thread "main" java.util.EmptyStackException
    at java.util.Stack.peek(Unknown Source)
    at java.util.Stack.pop(Unknown Source)
    at eval.main(eval.java:31)