- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的程序采用后缀表达式并将其更改为中缀表达式。
我在代码中包含了两个错误原因,即程序没有足够的运算符以及输入不是有效的数字或运算符。
当我输入不好的输入时,错误会被捕获,但是,当在扫描仪中输入正确的输入时,它会出现以下错误:
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:102)
at java.util.Stack.pop(Stack.java:84)
at PostfixToInfix.change(PostfixToInfix.java:67)
at PostfixToInfix.main(PostfixToInfix.java:27)
我的代码需要更改什么?代码:
import java.util.Scanner;
import java.util.Stack;
import java.util.EmptyStackException;
public class PostfixToInfix
{
int x = 0;
public static void main(String[] args)
{
PostfixToInfix exp = new PostfixToInfix();
Scanner stdin =new Scanner(System.in);
try {
boolean inputNeeded = true;
int value = 0;
while(inputNeeded){
System.out.print("Postfix : ");
if(stdin.hasNextInt()){
inputNeeded = false;
}
else{
throw new Error("Not a number or valid operator");
}
}
String pf = stdin.nextLine().replaceAll("\\s+", "");
System.out.println("Infix : "+exp.change(pf));
}
catch (EmptyStackException e) {
System.out.println("Too few operators to produce a single result.");
}
}
static boolean isOperator(char c)
{
if(c == '+' || c == '-' || c == '*' || c =='/' || c == '^')
{
return true;
}
return false;
}
boolean empty() //whether the stack is empty
{
return x == 0;
} // end empty
public String change(String pf)
{
Stack<String> s = new Stack<>();
for(int i = 0; i < pf.length(); i++)
{
char z = pf.charAt(i);
if(isOperator(z))
{
String x = s.pop();
String y = s.pop();
s.push("("+y+z+x+")");
}
else
{
s.push(""+z);
}
}
return s.pop();
}
}
最佳答案
考虑输入1 1 +
。
1
并将其存储在value
中。"1+"
) 存储在 pf
中,并作为参数传递给 change
方法。charAt
返回 pf
的第一个字符('1'
),isOperator
返回 false,然后执行 else block ,将 "1"
压入堆栈。charAt
返回 pf
的第二个字符('+'
),isOperator
返回 true,然后执行 if block 。pop
被调用一次,删除堆栈中唯一的元素 "1"
,并将其分配给 x
。堆栈现在为空,第二次调用 pop
会导致 EmptyStackException
。如果您的 IDE 还没有调试器,这是调试代码的方法。通过这一点,您应该发现使用 nextInt
是问题所在,因为当 if block 期望有两个数字时,剩余字符串中只会出现一个数字。
关于java - 是什么导致我收到此 EmptyStackException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191972/
我有以下部分代码来初始化堆栈数组: vDec.setRoute(new Stack[vDec.getIndexes().length]); for(i=0;i(); } 当我检查 while 循环内是
我的程序采用后缀表达式并将其更改为中缀表达式。 我在代码中包含了两个错误原因,即程序没有足够的运算符以及输入不是有效的数字或运算符。 当我输入不好的输入时,错误会被捕获,但是,当在扫描仪中输入正确的输
当我尝试运行后缀表达式时,由于我的字符串有两个背靠背的操作数,因此出现 EmptyStackException。我不知道如何在我的堆栈中捕获它。任何想法或想法都值得赞赏。 import java.ut
我正在用 Java 构建一个 treap 类。下面是我向陷阱添加新节点的函数。过程是:向下遍历到treap的底部(同时将路径中的每个节点添加到本地堆栈)首先只关心BST结构,然后,一旦到达底部,我将通
下面只是我的代码的一小块,我试图理解为什么如果我添加注释掉的代码行,它会抛出 EmptyStackException。如果一行中有两个空值(通过调用 items.getNextItem( 生成)),我
https://www.hackerrank.com/challenges/counting-valleys/problem存在计数谷问题我知道我的不是最好的解决方案,但它在我的系统上使用示例测试用例
我很困惑为什么当我将元素插入 ArrayList 时会抛出异常...这一定是我的 Push() 的问题方法,有人能找到问题吗?我尝试在 if 语句周围加上大括号,但没有成功,甚至可能是 empty()
我在应用程序中使用抛出 java.util.EmptyStackException 的第 3 方库 这只发生在虚拟机 JIT/预热期间 开始申请 开始压力测试,不增加压力。 抛出 java.util.
我是新来的,我有一个问题。我正在尝试实现一个比较器来按顶部比较两个堆栈。代码如下所示 class Comp implements Comparator> { @Override pub
我正在用 Java 实现 Stack。对于 pop 操作,我想抛出 EmptyStackException。根据documentation 、 EmptyStackException 有一个构造函数,
我想做的是让 eval 变量将它的每个字母放入堆栈中,然后打印出来。我收到 EmptyStackException 错误(假设这意味着堆栈中没有任何内容)。我不明白的是,我以为eval字符串被放入了变
我有一个由多个线程处理的 Stack 对象。其中一个线程是工作线程,它对 Stack 对象执行弹出操作。我想处理 Stack 为空的情况,我看到两个选项 try{ Object obj = (
下面是一段java代码,它接受中缀格式的表达式字符串并将其转换为后缀格式。 package post; import java.util.Stack; public class InfixtoPost
我有一个在 Oracle weblogic 中运行的 Java 项目。我们正在使用 apache-commons-chains。当尝试解析 XML 时,我们会间歇性地收到 EmptyStackExce
我正在 3 x 3 网格布局面板中创建一个数字益智游戏。首先我随机 8 个数字并将它们压入堆栈,然后弹出它们的值并将它们设置为按钮文本。应用程序运行良好,但有时我会打印出以下错误: Exception
我是一名优秀的程序员,十分优秀!