【问题标题】:How to handle a emptycollectionsException that doesn't result in an error?如何处理不会导致错误的 emptycollectionsException?
【发布时间】:2020-12-30 19:27:06
【问题描述】:

今天我有一个关于代码中的 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.");
      
    }

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

【问题讨论】:

    标签: java arrays exception crash stack


    【解决方案1】:
    1. 在调用 pop() 之前检查 isEmpty()

    1. 处理异常:

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

    【讨论】:

    • 在 isEmpty 类中,我究竟会将这个 do while 循环放在哪里?
    • 这意味着你已经在你的'main'中得到了do-while循环。只需在循环内滑动 try-catch 结构即可。
    • 哦哦。谢谢您的帮助!我终于成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多