2013-02-23 258 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