Java 中的 Stack 的 EmptyStackException 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13693707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EmptyStackException error with Stack in Java
提问by iii
What I'm trying to do is to get the eval variable to have each of its letters put into a stack and then printed out. I'm getting an EmptyStackException error(assuming that means that there isn't anything in the stack). What I don't understand is that I thought that the eval string was put into the variable stack. Why is it empty?
我想要做的是让 eval 变量将每个字母放入堆栈然后打印出来。我收到了一个 EmptyStackException 错误(假设这意味着堆栈中没有任何东西)。我不明白的是,我认为 eval 字符串被放入变量堆栈中。为什么是空的?
public static void main(String[] args)
{
Stack<String> variable = new Stack<String>();
String eval = StdIn.readString();
String alphabet = "abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < eval.length(); i++)
{
eval = eval.substring(i,i);
if (eval.equals(alphabet.substring(0, 52)))// checks if eval is equal to any letter of alphabet
{
variable.push(eval);
System.out.println(variable.pop());
}
}
}
}
Im using eclipse
我正在使用日食
Sample Runs:
示例运行:
input: hello
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Unknown Source)
at java.util.Stack.pop(Unknown Source)
at eval.main(eval.java:31)
回答by Phil K
There's a few issues I can see:
我可以看到几个问题:
eval.substring(i,i);
will return an empty string every time. You wanteval.substring(i,i + 1);
, or even better,eval.charAt(i);
.You'll want to put the returned substring/charAt character in it's own variable within your
for
loop. Currently it is overriding theeval
string after the first iteration.if (eval.equals(alphabet.substring(0, 52)))
doesn't do what you seem to think it does judging by your comment. If you want to check if a string contains another string (or even just a single character), use the methods:String#contains
orString#indexOf
.
eval.substring(i,i);
每次都会返回一个空字符串。你想要eval.substring(i,i + 1);
,甚至更好,eval.charAt(i);
。您需要将返回的 substring/charAt 字符放在
for
循环中它自己的变量中。目前它eval
在第一次迭代后覆盖字符串。if (eval.equals(alphabet.substring(0, 52)))
根据您的评论判断,它并没有做您认为的事情。如果您想检查一个字符串是否包含另一个字符串(甚至只是一个字符),请使用以下方法:String#contains
或String#indexOf
。
Here's a simple corrected snippet:
这是一个简单的更正片段:
String alphabet = "abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String eval = "blah";
Stack<Character> chars = new Stack<Character>();
for(char c : eval.toCharArray()) {
if(alphabet.indexOf(c) != -1) {
chars.push(c);
System.out.println(chars.pop());
}
}
回答by dreamcrash
The only way that you are getting this error is if:
您收到此错误的唯一方法是:
for (int i = 0; i < eval.length(); i++)
{
eval = eval.substring(i,i);
if (eval.equals(alphabet.substring(0, 52)))
{
variable.push(eval);
}
System.out.println(variable.pop());
}
If you have the System.out.println(variable.pop());
outside of the if condition.
如果你有System.out.println(variable.pop());
if 条件的 外部。
public Object pop()
Removes the object at the top of this stack and returns that object as the value of this function.
Returns: The object at the top of this stack (the last item of the Vector object). Throws:
移除此堆栈顶部的对象并将该对象作为此函数的值返回。
返回: 此堆栈顶部的对象(Vector 对象的最后一项)。抛出:
EmptyStackException- if this stack is empty.
EmptyStackException- 如果此堆栈为空。
In the code that you posted this is not possible since you have the same number of push
and pop
, and the push
operation comes first than pop
.
在您发布的代码中,这是不可能的,因为您有相同数量的push
and pop
,并且push
操作先于pop
。
The code you have put:
你放的代码:
the error you are getting:
你得到的错误:
回答by James McCracken
You need to check that each letter in eval
is a letter before you push it onto the stack. After you go through all characters in eval
, pop everything off the stack and print it out.
eval
在将其压入堆栈之前,您需要检查其中的每个字母是否都是一个字母。在浏览完 中的所有字符后eval
,将所有内容从堆栈中弹出并打印出来。
for(int i = 0; i < eval.length(); i++) {
if(alphabet.contains(eval.charAt(i))) {
variable.push(eval.charAt(i));
}
}
while(!variable.isEmpty()) {
System.out.println(variable.pop());
}
回答by Derek W
This should resolve your issue: You should only be popping after you push. Thus, they should both be inside the if-statement.
这应该可以解决您的问题:您应该只在推送后弹出。因此,它们都应该在 if 语句中。
for (int i = 0; i < eval.length(); i++)
{
if (alphabet.contains(eval.substring(i,i+1))
{
variable.push(eval.substring(i,i+1));
System.out.println(variable.pop());
}
}
回答by didierc
You want to check whether the characters in a string read from input are letters, and put them in a stac
You should consider using the Character classwhich let you perform all sort of tests on characters, without having to worry about encoding. The method that might be useful in your case is isLetter
, which check if the character is a letter.
您想检查从输入中读取的字符串中的字符是否为字母,并将它们放入 stac 您应该考虑使用Character 类,它可以让您对字符执行各种测试,而不必担心编码。在您的情况下可能有用的方法是isLetter
,它检查字符是否为字母。
As for the stack issue, your code actually push a value on top of the stack, and then immediately pop
it of, which explains why your stack is empty at the end of the loop. You should use the peek
method instead.
至于堆栈问题,您的代码实际上将一个值压入堆栈顶部,然后立即将pop
其推入,这解释了为什么您的堆栈在循环结束时为空。您应该改用该peek
方法。