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

huangapple go评论102阅读模式
英文:

How to handle a emptycollectionsException that doesn't result in an error?

问题

今天我有一个关于代码中的EmptyCollectionException的问题。以下是重要部分:

  1. public static void main (String [] args) //程序的主类
  2. {
  3. Scanner input = new Scanner(System.in);
  4. ArrayStack<String> stk = new ArrayStack<String>();
  5. int menu = 0; //初始化菜单
  6. do {
  7. System.out.println("堆栈菜单选项\n1.入栈 \n2.出栈 \n3.查看栈顶元素 \n4.显示栈 \n5.退出");
  8. System.out.println();
  9. System.out.print("请输入您的选择:");
  10. menu = Integer.parseInt(input.next()); //允许用户输入选择。
  11. switch (menu) {
  12. case 1: //如果选择了1,则用户可以将元素入栈。
  13. System.out.print("请输入元素:");
  14. String element = input.next();
  15. stk.push(element);
  16. break;
  17. case 2: //如果选择了2,则从栈顶弹出元素。
  18. System.out.println("弹出的元素为:" + stk.pop());
  19. break;
  20. case 3: //如果选择了3,则查看栈顶元素但不删除。
  21. System.out.println("栈顶元素为:" + stk.peek());
  22. break;
  23. case 4: //如果选择了4,则显示整个栈。
  24. System.out.println("完整的栈为:\n" + stk);
  25. break;
  26. default:
  27. System.out.println("选择了退出,程序正在关闭。"); //关闭程序
  28. return;
  29. }
  30. } while (true); //只要输入了1-5,程序就会循环。
  31. }

我遇到的错误是,当我尝试从空栈执行“出栈”操作时,会出现以下错误:

  1. 堆栈菜单选项
  2. 1.入栈
  3. 2.出栈
  4. 3.查看栈顶元素
  5. 4.显示栈
  6. 5.退出
  7. 请输入您的选择:2
  8. Exception in thread "main" jsjf.exceptions.EmptyCollectionException: 栈为空。
  9. at jsjf.ArrayStack.pop(ArrayStack.java:57)
  10. at jsjf.ArrayTest.main(ArrayTest.java:39)
  11. C:\Users\ADAIS\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java 返回了:1
  12. 构建失败(总时间:3秒)

我知道是什么导致了这个错误,是Pop和Peek代码在使用EmptyCollectionException类:

  1. public T pop() throws EmptyCollectionException //从栈顶移除元素并返回引用。如果栈为空,则抛出EmptyStackException。
  2. {
  3. if (isEmpty())
  4. throw new EmptyCollectionException("stack");
  5. top--;
  6. T result = stack[top];
  7. stack[top] = null;
  8. return result;
  9. }
  10. public T peek() throws EmptyCollectionException //返回栈顶元素的引用。不从栈顶移除元素。如果栈为空,则抛出EmptyCollectionException。
  11. {
  12. if (isEmpty())
  13. throw new EmptyCollectionException("stack");
  14. return stack[top-1];
  15. }

而EmptyCollectionException类是这样的:

  1. public EmptyCollectionException(String collection)
  2. {
  3. super("The " + collection + " is empty.");
  4. }

我想知道是否有人知道如何使它不会崩溃,而是循环显示“栈为空,请重试”之类的消息?这个代码的任务已经完成了,我只是想为了自己而修复它。

英文:

Today I have a question regarding the emptycollectionsException in my code. Here's the important bits

  1. public static void main (String [] args) //Main class of the program
  2. {
  3. Scanner input = new Scanner(System.in);
  4. ArrayStack&lt;String&gt; stk = new ArrayStack&lt;String&gt;();
  5. int menu = 0; //Initializes menu
  6. do {
  7. System.out.println(&quot;Stack Menu Selections\n1.Push \n2.Pop \n3.Peek \n4.Display \n5.Exit&quot;);
  8. System.out.println();
  9. System.out.print(&quot;Enter your Choice: &quot;);
  10. menu =Integer.parseInt(input.next()); //Allows the user to input a selection.
  11. switch (menu) {
  12. case 1: //If 1 is selected then the user can push an element into the stack.
  13. System.out.print(&quot;Enter element: &quot;);
  14. String element = input.next();
  15. stk.push(element);
  16. break;
  17. case 2: //If 2 is selected then the element at the top is popped from the stack.
  18. System.out.println(&quot;Popped Element is &quot; + stk.pop());
  19. break;
  20. case 3: //If 3 is selected then the top element is peeked but not deleted.
  21. System.out.println(&quot;Peeking is &quot; + stk.peek());
  22. break;
  23. case 4: //If 4 is selected, then the full stack is displayed.
  24. System.out.println(&quot;Full Stack is: \n&quot; + stk);
  25. break;
  26. default: System.out.println(&quot;Exit selected, shutting down program.&quot;); //Closes program
  27. return;
  28. }
  29. }while(true); //Program loops as long as 1-5 are inputed.
  30. }

The error I'm facing is that when I go to "pop" an empty stack, it results in this

  1. Stack Menu Selections
  2. 1.Push
  3. 2.Pop
  4. 3.Peek
  5. 4.Display
  6. 5.Exit
  7. Enter your Choice: 2
  8. Exception in thread &quot;main&quot; jsjf.exceptions.EmptyCollectionException: The stack is empty.
  9. at jsjf.ArrayStack.pop(ArrayStack.java:57)
  10. at jsjf.ArrayTest.main(ArrayTest.java:39)
  11. C:\Users\ADAIS\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
  12. BUILD FAILED (total time: 3 seconds)

I know what's causing it, an EmptyCollectionsException class that the Pop and Peek code is using

  1. public T pop() throws EmptyCollectionException //Removes the element that&#39;s at the top
  2. //of the stack and returns with a reference
  3. //to it. Throws an EmptyStackException if the
  4. //stack is empty
  5. {
  6. if (isEmpty())
  7. throw new EmptyCollectionException(&quot;stack&quot;);
  8. top--;
  9. T result = stack[top];
  10. stack[top] = null;
  11. return result;
  12. }
  13. public T peek() throws EmptyCollectionException //Returns a reference to the element that&#39;s
  14. //at the top of the stack. The element is
  15. //not removed from the top of the stack.
  16. //throws an EmptycollectionException is the stack is empty
  17. {
  18. if (isEmpty())
  19. throw new EmptyCollectionException(&quot;stack&quot;);
  20. return stack[top-1];
  21. }

which is

  1. public EmptyCollectionException(String collection)
  2. {
  3. super(&quot;The &quot; + collection + &quot; is empty.&quot;);
  4. }

I was wondering if anyone had any breadcrumbs on how to make it so that instead of crashing, it loops with a "Stack is empty, try again" kinda thing? The assignment for this code is already finished, I just wanna fix it for my own sake.

答案1

得分: 0

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

  1. 处理异常:
  1. do {
  2. try {
  3. switch (…) {
  4. ….blah blah blah
  5. }
  6. }
  7. catch (EmptyCollectionException ex) {
  8. System.out.println("Nothing to pop");
  9. }
  10. } while (true);
英文:
  1. Check isEmpty() before calling pop()

or

  1. Handle the exception:

    1. do {
    2. try {
    3. switch (…) {
    4. ….blah blah blah
    5. }
    6. }
    7. catch (EmptyCollectionException ex) {
    8. System.out.println(&quot;Nothing to pop&quot;);
    9. }
    10. } while (true);

huangapple
  • 本文由 发表于 2020年9月12日 22:10:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63861180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定