2013-02-23 177 views
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

另一个字符串平等的问题,在这里你去...

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

应该

("0".equals(str) 

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

3

或者说使用"0".equals(str)代替str.equals("0"),因为如果str为null最后一个将失败(空指针异常)

干杯!

+0

好记录下来! – Parth 2013-02-23 19:01:13