NumberFormatException.for InputString:“(”

时间:2019-05-03 23:01:32

标签: java string multithreading exception main

运行代码时,我遇到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;

    }

}

}

2 个答案:

答案 0 :(得分:0)

NumberFormatException

  

抛出该应用程序   试图将字符串转换为数字类型之一,但是   该字符串的格式不正确。

public static int parseInt(String s)

  

将字符串参数解析为带符号的十进制整数。那些角色   字符串中的所有字符必须全部为十进制数字,但第一个除外   字符可以是ASCII减号'-'('\ u002D'),以表示   负值或ASCII加号'+'('\ u002B')表示   正值。返回结果整数值,就像   参数和基数10作为参数给出   parseInt(java.lang.String,int)方法。

“(”不是数字。

是的,Famalam Fammy!

答案 1 :(得分:0)

公共类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);

    }

}