I wrote a code for the basic operation of the stack, but there has been a problem with exception handling. I don't know how to solve it.

import java.util.EmptyStackException;
import java.util.Stack;

public class ArrayStack<E> implements TestStack<E>{

    private E[] theArray;
    private int topOfStack;

    public static final int DEFAULT_CAPACITY = 20;

    public ArrayStack(){

        theArray = (E[])new Object[DEFAULT_CAPACITY];
        topOfStack = -1;

    }
    //overwrite the push meathod
    public void push(E data){
        if (topOfStack == theArray.length-1);
        {
            doubleArray();
        }
        topOfStackPP;
        theArray[topOfStack]=data;

    }

    @Override
    public E pop() throws EmptyStackException {
        if(empty()){
            throw new EmptyStackException("Stack pop");

        }
        E result = theArray[topOfStack];
        topOfStack--;
        return result;
    }

    @Override
    public E peek() throws EmptyStackException {
        if (empty()){
            throw new EmptyStackException("Stack peek");
        }
        return theArray[topOfStack];
    }

    @Override
    public boolean empty() {
        return topOfStack == -1;
    }

    @Override
    public int size() {
        return topOfStack + 1;
    }


    private void doubleArray(){
        E[] temp =theArray;
        theArray = (E[]) new Object[temp.length*2];

        for(int i=0; i<temp.length;iPP){
            theArray[i]=temp[i];
        }

    }



}

in throw new EmptyStackException ("Stack peek") here and above. ("Stack pop") are all reporting errors, and the error says EmptyStackException () in EmptyStackException cannot be applied to.. . This is very strange. I don"t know what the error is,

.
Apr.21,2022

two errors:
1 pop or peek you should push data before
2 EmptyStackException does not have a constructor with String, throw new EmptyStackException ("Stack peek"); and throw new EmptyStackException ("Stack pop"); should be changed to throw new EmptyStackException ();


you should not put anything in it


the definition of the class EmptyStackException is as follows:

public
class EmptyStackException extends RuntimeException {
    private static final long serialVersionUID = 5084686378493302095L;

    /**
     * Constructs a new EmptyStackException with <tt>null</tt>
     * as its error message string.
     */
    public EmptyStackException() {
    }
}

does not have a constructor that takes a string as an argument, so you cannot add a string parameter when new .

Menu