0

我正在为后缀翻译器/计算器编写中缀。我正在从输入文件中读取并尝试匹配读取的字符串。我知道我将正确的字符串传递给方法,正如用于测试的打印语句所看到的那样。我只是无法弄清楚为什么不满足 if 语句的条件!

static int j = 1;

    public static void readMath(String str, myStack s, myQueue q) {
            System.out.println("\n~~~round "+j+"~~~ str=="+str);//<--this line confirms that the correct string is being passed in
           //for example: if "1" is passed in, the first if statement's conditions are failing to be met
            j++;

            if (str == "0" || str == "1" || str == "2" || str == "3" || str == "4" || str == "5" || str == "6" || str == "7" || str == "8" || str == "9") {
                System.out.println(">NUMBER"); // <--for testing.
                q.enqueue(str);
            } else if(str == "+" || str=="-") {
                System.out.println("> + or -");
                String x = (String)s.pop();
                String y = x;
                while( !s.isEmpty() && !(x == "<" || x == ">" || x == "&" || x == "|" || x =="=")) {
                    q.enqueue(x);
                    x = (String) s.pop();
                }
                if(x == "<" || x == ">" || x == "&" || x == "|" || x == "=") {
                    q.enqueue(x);
                    s.push(y);
                }
            } else if(str == "<" || str == ">") {
                System.out.println(">GT or LT"); // <--for testing.
                String x = (String) s.pop();
                String y = x;
                while( !s.isEmpty() && !(x == "&" || x == "|" || x == "=")) {
                    q.enqueue(x);
                    x = (String) s.pop();
                }
                if(x == "&" || x == "|" || x == "=") {
                    q.enqueue(x);
                    s.push(y);
                }
            } else if(str == "=") {
                System.out.println("> ="); // <--for testing.
                String x = (String) s.pop();
                String y = x; 
                while( !s.isEmpty() && !(x == "&" || x == "|")) {
                    q.enqueue(x);
                    x = (String) s.pop();
                }
                if(x == "&" || x == "|") {
                    q.enqueue(x);
                    s.push(y);
                }
            } else if(str == "&" || str == "|") {
                System.out.println("> & or |"); // <--for testing.
                String x = (String) s.pop();
                String y = x; 
                while( !s.isEmpty() && !(x == "!" || x == "&" || x == "|")) {
                    q.enqueue(x);
                    x = (String) s.pop();
                }
            } else if(str=="/" || str == "*") {
                System.out.println(">divide or multiply"); // <--for testing.
                String x = (String) s.pop();
                String y = x; 
                while( !s.isEmpty() && !(x == "&" || x == "|" || x == "=" || x == "<" || x == ">" || x == "+" || x == "-")) {
                    q.enqueue(x);
                    x = (String) s.pop();
                }
                if(x == "&" || x == "|" || x == "=" || x == "<" || x == ">" || x == "+" || x == "-") {
                    q.enqueue(x);
                    s.push(y);
                }
            } else if(str == ")") {
                System.out.println(">close paren"); // <--for testing.
                String x = (String) s.pop();
                while( !s.isEmpty() && x != "(" ) {
                    q.enqueue(x);
                    x = (String) s.pop();
                }
            }
        s.printStack();
        q.printQueue();     
    }

    public static myStack s;
    public static myQueue q;


    public static void readMathFile() {
        s = new myStack();
        q = new myQueue();
        File afile = new File ("/Users/tteumer2010/Documents/java/Project1/src/test.txt");
        FileReader fileread = null;

        try { fileread = new FileReader(afile); }
        catch (FileNotFoundException e) {   e.printStackTrace(); }

        BufferedReader bufread = new BufferedReader(fileread);
        String str = new String();
        try {
            while((str = bufread.readLine()) != null) {
                String[] a = parse(str);
                for(int i = 0; i < a.length; i++) {
                    System.out.println(a[i]);
                    readMath(a[i], s, q);
                }
            }
        } catch (IOException e) { e.printStackTrace(); }
    }

    public static String[] parse(String s) {
        String[] str = s.split(" ");
        return str;
    }
4

2 回答 2

4

另一个字符串相等问题,给你...

(str == "0" and all the string equality compariosions in your code

应该

("0".equals(str)

使用equals()方法检查字符串是否相等==运算符检查两个字符串引用是否引用同一个字符串对象。

于 2013-02-23T18:56:01.553 回答
3

或者更确切地说使用"0".equals(str)代替str.equals("0"),因为如果 str 为 null 最后一个将失败(NullPointerException)

干杯!

于 2013-02-23T19:00:12.123 回答