My custom made Stack class is always throwing StackIsUnderflowing Exception : Why?

75 Views Asked by At

I have a custom made Stack class that doesn't manipulate arrays but ArrayList<String>.

My Stack class has push, pop and size methods.

I am tracking the index through an index instance variable.

I created an array to hold three elements.

These three elements are pushed onto Stack. And finally, the last element is popped out and printed on the console.

I am always getting custom StackIsUnderflowing() Exception.

Can anyone kindly tell me where the error is?

import java.util.ArrayList;


class StackIsUnderflowing extends Exception {

    private static final long serialVersionUID = 1 L;

    public StackIsUnderflowing() {
        super("Stack Underflowing");
    }
}

//main Stack class
public class Stack {
    private int index;
    //List of String literals
    private ArrayList < String > aStackArr = new ArrayList < String > ();
    private static int ZERO = 0;

    //constructor
    public Stack() {

        this.index = -1;
    }

    //return the size
    public int size() {
        return aStackArr.size();
    }

    //push value on to Stack add value to List of String input variable
    public void push(String anInt) {
        index = aStackArr.size() - 1;

        aStackArr.add(anInt);

        System.out.println(index + aStackArr.get(index));

    }

    //pop values from input variable
    public String pop() {

        try {
            if (index < Stack.ZERO) {
                throw new StackIsUnderflowing();
            } else {
                String result = aStackArr.remove(index);

                index = aStackArr.size() - 1;
                return result;
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.exit(-1);
            return "-1";
        }
    }

    //peek into LIFO top input literal

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Stack aStack = new Stack();
        String[] names = new String[3];
        names[0] = "ll";
        names[1] = "cc";
        names[2] = "dd";
        String tmp;

        for (int i = 0; i < aStack.size(); ++i) {
            System.out.println(names[i]);
            aStack.push(names[i]);


        }
        tmp = aStack.pop();

        System.out.println(tmp);
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

The problem is that you assign index = aStackArr.size() - 1; before aStackArr.add(anInt);. The method push(String anInt) should be like this:

//push value on to Stack add value to List of String input variable
    public void push(String anInt)
    {
        aStackArr.add(anInt);
        index = aStackArr.size() - 1;
        System.out.println(index + aStackArr.get(index));
    }

Also, the is a problem with the code that adds pushes the strings, the loop is on aStack.size() instead of names.length

Here is the full code (formatted)

import java.util.ArrayList;

class StackIsUnderflowing extends Exception
{

    private static final long serialVersionUID = 1L;

    public StackIsUnderflowing()
    {
        super("Stack Underflowing");
    }
}

//main Stack class
public class Stack
{
    private int index;
    //List of String literals
    private ArrayList<String> aStackArr = new ArrayList<String>();
    private static int ZERO = 0;

    //constructor
    public Stack()
    {
        this.index = -1;
    }

    //return the size
    public int size()
    {
        return aStackArr.size();
    }

    //push value on to Stack add value to List of String input variable
    public void push(String anInt)
    {
        aStackArr.add(anInt);
        index = aStackArr.size() - 1;
        System.out.println(index + aStackArr.get(index));
    }

    //pop values from input variable
    public String pop()
    {
        try
        {
            if (index < Stack.ZERO)
            {
                throw new StackIsUnderflowing();
            }
            else
            {
                String result = aStackArr.remove(index);
                index = aStackArr.size() - 1;
                return result;
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(-1);
            return "-1";
        }
    }

    //peek into LIFO top input literal

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        Stack aStack = new Stack();
        String[] names = new String[3];
        names[0] = "ll";
        names[1] = "cc";
        names[2] = "dd";
        String tmp;

        for (int i = 0; i < names.length; ++i)
        {
            System.out.println(names[i]);
            aStack.push(names[i]);
        }
        tmp = aStack.pop();
        System.out.println(tmp);
    }
}