空堆栈异常! [英] Empty stack exception!

查看:91
本文介绍了空堆栈异常!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.*;

public class BiggerStackProject
{
   public static void main(String args[]){
      String tester = "5+8-6*8";
      Stack<string> test = stackTheString(tester);
      int size = test.size();
      System.out.println(mathTheStack(test));
   }

   public static Stack<string> stackTheString(String input){
      Stack<string> original = new Stack<string>();
      String superTemp = null;
      for(int x = 0; x < input.length(); x++){
         String temp = Character.toString(input.charAt(x));
         original.push(temp);
      }
      return original;
   }

   public static double mathTheStack(Stack<string> input){
      Stack<string> operators = new Stack<string>();
      Stack<string> numbers = new Stack<string>();
      for(int x = 0; x < input.size(); x++){
         if(!(input.peek() == "+" || input.peek() == "-" || input.peek() == "*" || input.peek() == "/" || input.peek() == "%")){
            numbers.push(input.pop());
         }
      }
      for(int x = 0; x < input.size(); x++){
         if(input.peek() == "*" || input.peek() == "/" || input.peek() == "%"){
            operators.push(input.pop());
         }
         else{
            if(operators.pop().equals("*")){
               numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) * Double.parseDouble(numbers.pop())));
            }
            else if(operators.pop().equals("/")){
               numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) / Double.parseDouble(numbers.pop())));
            }
            else if(operators.pop().equals("+")){
               numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) + Double.parseDouble(numbers.pop())));
            }
            else if(operators.pop().equals("-")){
               numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) - Double.parseDouble(numbers.pop())));
            }
            else if(operators.pop().equals("%")){
               numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) % Double.parseDouble(numbers.pop())));
            }
         }
      }
      return Integer.parseInt(numbers.pop());
   }
}





我继续这样:



I Keep Getting this:

Exception in thread "main" java.util.EmptyStackException
	at java.util.Stack.peek(Stack.java:85)
	at java.util.Stack.pop(Stack.java:67)
	at BiggerStackProject.mathTheStack(BiggerStackProject.java:32)
	at BiggerStackProject.main(BiggerStackProject.java:8)

Process completed.

< br $> b $ b

我尝试过的事情:



我无法解决任何问题。



What I have tried:

I just cant figure anything out.

推荐答案

您应该在 mathTheStack 方法的开头放置一个断点并启动调试会话;这将允许您检查三个堆栈并理解为什么BiggerStackProject.java文件的第32行导致抛出异常。

快速检查您的算法,我认为这部分可能是有意义的:

You should put a breakpoint at the beginning of your mathTheStack method and launch a debug session; this will allow you to check the three stacks and to understand why the line 32 of the BiggerStackProject.java file causes the exception to be thrown.
Quickly checking your algorithm, I think this part may be of interest:
for(int x = 0; x < input.size(); x++){
         if(input.peek() == "*" || input.peek() == "/" || input.peek() == "%"){
            operators.push(input.pop());
         }
         else{
            if(operators.pop().equals("*")){
               // ...



在第一次迭代(x = 0),如果第一个字符是数字,执行else块。此时,您还没有将任何内容推入运算符堆栈,并且您尝试从其中弹出,这会抛出异常。

有几种方法可以实现您想要实现的目标,但是您可以尝试的第一件事是在尝试使用它们之前首先对数字和运算符堆栈进行限定。你只能在你的第一个循环中限定数字堆栈,所以也许你可以重做这个循环来限定两个堆栈?



作为旁注,为什么你要处理数字字符串? Stack 类是通用的,这意味着你也可以使用 Stack< double>


On the first iteration (x = 0), if the first character is a digit, the else block is executed. At this point, you did not push anything into the operators stack yet, and you try to pop from it, which throws the exception.
There are several ways to do what you are trying to achieve, but one of the first things you could try is to qualify both numbers and operators stacks first, before trying to use them. You only qualified the numbers stack in your first loop, so maybe you could rework this loop to qualify both stacks?

As a side note, why do you handle numbers as strings? Stack class is generic, meaning you can use a Stack<double> as well.


如果你的代码中的 operators.pop()错误,我觉得你喜欢ysage:

I feel likeyour ysage if operators.pop() is wrong in your code:
if(operators.pop().equals("*")){
   numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) * Double.parseDouble(numbers.pop())));
}
else if(operators.pop().equals("/")){
   numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) / Double.parseDouble(numbers.pop())));
}
else if(operators.pop().equals("+")){
   numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) + Double.parseDouble(numbers.pop())));
}



你需要了解你对堆栈做了什么,调试器是唯一可以告诉你代码正在做什么的工具堆栈。



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


You need to understand what you do with the stack, the debugger is the only tool that can show you wgat your code is doing with the stack.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于空堆栈异常!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆