首页 > 解决方案 > 如何处理不会导致错误的 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.");
      
    }

我想知道是否有人对如何制作它有任何面包屑,这样它不会崩溃,而是循环显示“堆栈为空,再试一次”之类的东西?这段代码的任务已经完成,我只是为了我自己的利益而修复它。

标签: javaarraysexceptioncrashstack

解决方案


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

或者

  1. 处理异常:

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

推荐阅读