如何处理不导致错误的emptycollectionsException?

问题描述

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

int x = 5;
x = x + 5;
int diff = x - old_x;

我面临的错误是,当我“弹出”一个空堆栈时,会导致这种情况

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

我知道是什么原因造成的,这是Pop and Peek代码正在使用的EmptyCollectionsException类

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)

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];
    }

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

解决方法

  1. 在调用pop()之前检查isEmpty()

  1. 处理异常:

    do { 
        try { 
           switch (…) { 
              ….blah blah blah … 
           } 
       }
       catch (EmptyCollectionException ex) { 
           System.out.println("Nothing to pop"); 
       } 
    } while (true);