gpt4 book ai didi

java - 使用堆栈错误进行后缀评估(java)

转载 作者:太空宇宙 更新时间:2023-11-04 06:10:20 25 4
gpt4 key购买 nike

所以我正在编写一个程序来计算文件中的后缀表达式。但是我不断收到错误:

 0
Exception in thread "main" java.lang.NumberFormatException: For input string: "+"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:484)
at java.lang.Integer.valueOf(Integer.java:582)
at postfixEvaluater.postfixEvaluation(postfixEvaluater.java:89)
at postfixEvaluater.readFromTheFile(postfixEvaluater.java:49)
at postfixEvaluater.main(postfixEvaluater.java:21)

所以我猜测这和我的运算符(operator)有关系。

输入.txt文件内容如下:

 511+93/2-932/-149657%/*-+14+96*5/7%-149-+6*57%/ 

预期输出是:

 5
2
1
8
-4
2
-4

这是迄今为止我的所有代码:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class postfixEvaluater {
public static void main(String[] commandlineArguments) {
//Error Checking
if(commandlineArguments.length == 0){
System.out.println("Please enter the file name.");
}
else{
postfixEvaluater.readFromTheFile(commandlineArguments[0]);
}

}


public static void readFromTheFile(String fileName) {
//connect to file
File file = new File(fileName);
Scanner scanFile = null;
try {
scanFile = new Scanner(file);
}
catch (FileNotFoundException exception) {

System.out.print("File not found for " + fileName);

System.exit(1);
}

while (scanFile.hasNextLine()) {

String line = scanFile.nextLine();

Integer postfixLine = postfixEvaluater.postfixEvaluation(line);
System.out.println(postfixLine);



}
}


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);

Integer character = (Integer.valueOf(characterNum));



if (character.equals("0")) {
stack.push(character);

}
else if (character.equals("1")) {
stack.push(character);

}
else if (character.equals("2")) {
stack.push(character);
}
else if (character.equals("3")) {
stack.push(character);
}
else if (character.equals("4")) {
stack.push(character);
}
else if (character.equals("5")) {
stack.push(character);
}
else if (character.equals("6")) {
stack.push(character);
}
else if (character.equals("7")) {
stack.push(character);
}
else if (character.equals("8")) {
stack.push(character);
}
else if (character.equals("9")) {
stack.push(character);
}
else if (character.equals("*")) {
integer2 = stack.pop();
integer1 = stack.pop();


result = integer1 * integer2;


stack.push(result);



}
else if (character.equals("/")) {
integer2 = stack.pop();
integer1 = stack.pop();

result = integer1 / integer2;


stack.push(result);

}
else if (character.equals("%")) {
integer2 = stack.pop();
integer1 = stack.pop();


result = integer1 % integer2;


stack.push(result);
}
else if (character.equals("+")) {
integer2 = stack.pop();
integer1 = stack.pop();


result = integer1 + integer2;


stack.push(result);

}
else if (character.equals("-")) {
integer2 = stack.pop();
integer1 = stack.pop();

result = integer1 - integer2;


stack.push(result);

}


}
return result;
}
}

这是我觉得问题所在的代码部分(顺便说一句,我只推送和弹出整数):

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);

Integer character = (Integer.valueOf(characterNum));



if (character.equals("0")) {
stack.push(character);

}
else if (character.equals("1")) {
stack.push(character);

}
else if (character.equals("2")) {
stack.push(character);
}
else if (character.equals("3")) {
stack.push(character);
}
else if (character.equals("4")) {
stack.push(character);
}
else if (character.equals("5")) {
stack.push(character);
}
else if (character.equals("6")) {
stack.push(character);
}
else if (character.equals("7")) {
stack.push(character);
}
else if (character.equals("8")) {
stack.push(character);
}
else if (character.equals("9")) {
stack.push(character);
}
else if (character.equals("*")) {
integer2 = stack.pop();
integer1 = stack.pop();


result = integer1 * integer2;


stack.push(result);



}
else if (character.equals("/")) {
integer2 = stack.pop();
integer1 = stack.pop();

result = integer1 / integer2;


stack.push(result);

}
else if (character.equals("%")) {
integer2 = stack.pop();
integer1 = stack.pop();


result = integer1 % integer2;


stack.push(result);
}
else if (character.equals("+")) {
integer2 = stack.pop();
integer1 = stack.pop();


result = integer1 + integer2;


stack.push(result);

}
else if (character.equals("-")) {
integer2 = stack.pop();
integer1 = stack.pop();

result = integer1 - integer2;


stack.push(result);

}


}
return result;
}

最佳答案

您的问题是以下代码行:

Integer character = (Integer.valueOf(characterNum));

由于您尝试将任何字符转换为数字,并且您的字符串还包含运算符(无法转换为数字),因此您会遇到异常。

你可能应该这样考虑:

  • 我的角色是数字吗?

    是=>将其转换为数字并将其压入堆栈

    否 => 它是一个操作数,因此从堆栈中弹出顶部 2 个元素,执行操作并推送结果。

关于java - 使用堆栈错误进行后缀评估(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28826440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com