我要做的是让 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());
}
}
}
}
我使用日食
样品运行:
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)