emptycollectionsException

emptycollectionsException

今天,我对代码中的emptycollectionsException有疑问。这是重要的部分

public static void main (String [] args) //Main class of the program
   {
      Scanner input = new Scanner(System.in);
      ArrayStack<String> stk = new ArrayStack<String>();
      int menu = 0; //Initializes menu
              do {
                 System.out.println("Stack Menu Selections\n1.Push \n2.Pop \n3.Peek \n4.Display \n5.Exit");
                 System.out.println();
                 System.out.print("Enter your Choice: ");
                 menu =Integer.parseInt(input.next()); //Allows the user to input a selection.
                 switch (menu) {
                    case 1: //If 1 is selected then the user can push an element into the stack.
                       System.out.print("Enter element: ");
                       String element = input.next();
                       stk.push(element);
                       break;
                    case 2: //If 2 is selected then the element at the top is popped from the stack.
                       System.out.println("Popped Element is " + stk.pop());
                      break;
                    case 3: //If 3 is selected then the top element is peeked but not deleted.
                       System.out.println("Peeking is " + stk.peek());
                       break;
                    case 4: //If 4 is selected, then the full stack is displayed.
                       System.out.println("Full Stack is: \n" + stk);
                       break;
                    default: System.out.println("Exit selected, shutting down program."); //Closes program
                    return;
                 }
              }while(true); //Program loops as long as 1-5 are inputed.

              }
我面临的错误是,当我去“弹出”一个空堆栈时,它导致
Stack Menu Selections
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your Choice: 2
Exception in thread "main" jsjf.exceptions.EmptyCollectionException: The stack is empty.
    at jsjf.ArrayStack.pop(ArrayStack.java:57)
    at jsjf.ArrayTest.main(ArrayTest.java:39)
C:\Users\ADAIS\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)
我知道是什么原因造成的,Pop和Peek代码正在使用EmptyCollectionsException类
public T pop() throws EmptyCollectionException //Removes the element that's at the top
                                                  //of the stack and returns with a reference
                                                  //to it. Throws an EmptyStackException if the
                                                  //stack is empty
    {
        if (isEmpty())
            throw new EmptyCollectionException("stack");
        top--;
        T result = stack[top];
        stack[top] = null;
        return result;
    }
    public T peek() throws EmptyCollectionException //Returns a reference to the element that's
                                                   //at the top of the stack. The element is
                                                   //not removed from the top of the stack.
                                                   //throws an EmptycollectionException is the stack is empty
    {
        if (isEmpty())
            throw new EmptyCollectionException("stack");
        return stack[top-1];
    }
这是
public EmptyCollectionException(String collection)
    {
        super("The " + collection + " is empty.");

    }
我想知道是否有人对如何制作面包屑,以便它不会崩溃,而是循环出现“堆栈为空,请重试”之类的东西?这段代码的分配已经完成,我只是想为我自己解决它。

最佳答案

  • 在调用pop()
  • 之前检查isEmpty()

    要么
  • 处理异常:
    do {
        try {
           switch (…) {
              ….blah blah blah …
           }
       }
       catch (EmptyCollectionException ex) {
           System.out.println("Nothing to pop");
       }
    } while (true);
    
  • 08-28 20:49