在运行我的代码时,我将获得numberFormateXception:
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:569)
at java.lang.Integer.parseInt(Integer.java:615)
at collection.MyExpressionTrees.evaluateHelper(MyExpressionTrees.java:58)
at collection.MyExpressionTrees.evaluateHelper(MyExpressionTrees.java:78)
at collection.MyExpressionTrees.evaluate(MyExpressionTrees.java:50)
public class MyExpressionTrees extends MyBinaryTree {
public MyExpressionTrees() {
root = null;
}
public MyExpressionTrees(MyBinaryTreeNode rt) {
root = rt;
}
public int evaluate() {
if (root == null) {
return -1000000;
}
return evaluateHelper(root);
}
private int evaluateHelper(MyBinaryTreeNode rt) {
if (rt.left == null) {
return Integer.parseInt(rt.data.toString());
}
switch (rt.data.toString()) {
case "+":
return evaluateHelper(rt.left) + evaluateHelper(rt.right);
case "-":
return evaluateHelper(rt.left) - evaluateHelper(rt.right);
case "/":
return evaluateHelper(rt.left) / evaluateHelper(rt.right);
case "*":
return evaluateHelper(rt.left) * evaluateHelper(rt.right);
case "%":
return evaluateHelper(rt.left) % evaluateHelper(rt.right);
default:
return -1000000;
}
}
}
numberFormateXception:
投掷以指示应用程序 试图将字符串转换为数字类型之一,但是 字符串没有适当的格式。
public static int parseint(字符串S(:
将字符串参数解析为签名的十进制整数。那些角色 字符串中必须全部是十进制数字,除了第一个 字符可能是ASCII负符号' - '(' u002d'(,以指示一个 负值或ASCII加上符号' '(' u002b'(表示一个 正值。结果整数值返回,就像 论点和radix 10被作为论据 parseint(java.lang.string,int(方法。
"("不是数字。
是的,famalam fammy!
公共类Project_t {
private static final String fpath = "../COSC241_P4_Input.txt";
private static MyExpressionTree exTree = new MyExpressionTree();
private static List<String> infList;
private static final MyDeque d = new MyDeque();
private static final MyStack st = new MyStack();
private static int result;
/**
*
* Test method for the project. Handles file input and output, as well as *
* running tests on each expression in the input file. *
*/
public static void test() {
result = 0;
File out = new File("../COSC241_P4_Output_tdreed0.txt");
try {
//Stores each line of the file in a list.
infList = FP.readFile(fpath).stream().filter((s) -> !s.isEmpty()).collect(Collectors.toList());
//If the output file does not currently exist, create it.
if (!out.exists()) {
out.createNewFile();
}
try (PrintWriter writer = new PrintWriter(out)) {
for (String s : infList) {
String root = s.trim().replaceAll(" ", "").replaceAll("\[", "(").replaceAll("\]", ")");
writer.println("Original Infix:t" + root);
if (!ExpressionRegulators.allPMatched(root) || !ExpressionRegulators.allValid(root) || !ExpressionRegulators.isValidOrder(root)) {
writer.println("Invalid Expression");
writer.println();
} else {
buildExpressionTree(root);
//Performs traversals on the tree
exTree.preorderTraversal();
writer.println("Preorder: " + exTree.preorderStr);
exTree.inorderTraversal();
writer.println("Inorder: " + exTree.inorderStr);
exTree.postorderTraversal();
writer.println("Postorder: " + exTree.postorderStr);
result = exTree.evaluate();
writer.println("Result: " + result);
writer.println();
exTree.clear();
}
}
}
} catch (IOException e) {
System.out.println(e.toString());
System.exit(0);
}
}