所以我试图在我的Postfix表达式求值程序中创建一些try-catch语句来捕获将从输入文件中出现的异常。我只需要帮助制作那些try-catch语句。
所以当前的输出是:
5
2
1
8
-4
2
-4
Exception in thread "main" java.util.EmptyStackException
at LinkedStack.pop(LinkedStack.java:44)
at PostFix.postfixEvaluation(PostFix.java:171)
at PostFix.readFromTheFile(PostFix.java:49)
at PostFix.main(PostFix.java:21)
虽然我希望它输出:
5
2
1
8
-4
2
-4
ERROR: java.lang.RuntimeException: Bad character input: " "
ERROR: java.util.EmptyStackException for 1+1
ERROR: java.util.EmptyStackException for 11+*
ERROR: java.util.EmptyStackException for +11
ERROR: java.lang.RuntimeException: Bad character input: "@"
输入文件包含:
511+93/2-932/-149657%/*-+14+96*5/7%-149-+6*57%/ 1+111+*+1111@
这是我当前的代码方法,try-catch块将在这里:
public static Integer postfixEvaluation(String input){
StackInterface<Integer> stack = new LinkedStack<Integer>();
Integer result = new Integer(0);
for(int i=0;i<input.length();i++){
String characterNum = input.substring(i, i+1);
Integer integer1 = new Integer(0);
Integer integer2 = new Integer(0);
if (characterNum.equals("0")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("1")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("2")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("3")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("4")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("5")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("6")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("7")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("8")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("9")) {
Integer character = (Integer.valueOf(characterNum));
stack.push(character);
}
else if (characterNum.equals("*")) {
integer2 = stack.pop();
integer1 = stack.pop();
result = integer1 * integer2;
stack.push(result);
}
else if (characterNum.equals("/")) {
integer2 = stack.pop();
integer1 = stack.pop();
result = integer1 / integer2;
stack.push(result);
}
else if (characterNum.equals("%")) {
integer2 = stack.pop();
integer1 = stack.pop();
result = integer1 % integer2;
stack.push(result);
}
else if (characterNum.equals("+")) {
integer2 = stack.pop();
integer1 = stack.pop();
result = integer1 + integer2;
stack.push(result);
}
else if (characterNum.equals("-")) {
integer2 = stack.pop();
integer1 = stack.pop();
result = integer1 - integer2;
stack.push(result);
}
}
return result;
}
非常感谢任何帮助
答案 0 :(得分:1)
当你得到任何字符时,如果是,那么字符是空白然后通过你的自定义例外。
if(characterNum.equals("")){
throw new BadCharacterException(" Bad character input: \" \"");
}
答案 1 :(得分:0)
如果我理解你,那么您的例外情况应该在应用程序结束时分组。我认为你可以正常捕捉你的例外(例如atish shimpi answare),然后在catch部分通过将它们推送到某种fifo集合来处理异常。处理完整个输入后,只需将其打印出来。
我会在这里选择检查异常,并在打印它们的地方处理异常打印。您可以决定是否要停止,如何格式化消息等。