2014-09-05 86 views
0

我有一個野趣的臭蟲程序達到此行Java字符串比較有趣的bug

System.out.println("!tempLine.equals(raf.readLine().toString())"); 

每次在迴路中的隨機指數。 不知道tempLine如何可能不等於raf.readLine()。toString(),其中前一個分配始終處於平穩狀態。 另一個有趣的(不確定它是相關的)是在某些時候raf.readLine()和raf.readLine()。toString()有兩個不同的值。 Deperate幫忙:-)

private static Map<String, List<KeyPhraseAnnotation>> getKeyPhrasesFromNewNlp(String filename) throws Exception 
{ 
    String manualMemoPrefix = "Caller/Customer Name:"; 

    FreeTextProcessingPipeline nlpPipeline = springContext.getBean(FreeTextProcessingPipeline.class); 
    nlpPipeline.initialize(); 
    Map<String, List<KeyPhraseAnnotation>> kpMatrix = new HashMap<String, List<KeyPhraseAnnotation>>(); 

    //Map<String, FreeTextProcessingResult> results = nlpPipeline.processFiles(folder, "en-US"); 
    Random rand = new Random(); 
    BufferedReader br = new BufferedReader(new FileReader(filename)); 
    RandomAccessFile raf = new RandomAccessFile(filename,"rw"); 
    String line; 
    long counter = 0; 
    int lines = 0; 
    int k = 0; 
    int s_max = 0; 
    int s_min = 0; 
    int t = 0; 
    int hit=0; 
    double e ; 
    int max_num_of_documents = 500; 
    String[] parts = null; 

    while (br.readLine() != null) lines++; 

    t = (int) Math.floor((lines/max_num_of_documents)); 
    k = t; 
    e = 0.1 * k; 
    String tempLine = null; 
    String memo_manual = null; 
    int current_num_docs = 0; 
    while (current_num_docs<max_num_of_documents){ 

     System.out.println("this is u in beginning of loop: " + current_num_docs); 
     tempLine = null; 
     s_max = (int) (k+e); 
     s_min = (int) (k-e); 
     hit = s_min + (int)(Math.random() * ((s_max - s_min) + 1)) ; 

     if(hit<lines && hit>0){ 
     raf.seek(hit); 
     } 
     else{ 
     break; 
     } 

     tempLine = raf.readLine().toString(); 
     if (!tempLine.equals(raf.readLine().toString())) 
     { 
      System.out.println("!tempLine.equals(raf.readLine().toString())"); 
     } 
     parts = tempLine.split("\\|"); 
     //String sessionId = parts[0]; 
     if(parts.length == 21){ 
      memo_manual = parts[15]; 
     } 
     else { 
      memo_manual=""; 
      System.out.println(raf.readLine() + "    " + tempLine); 
     } 

     if (memo_manual.toLowerCase().contains(manualMemoPrefix.toLowerCase())){ 
      FreeTextProcessingRequest request = new FreeTextProcessingRequest(); 
      request.setText(memo_manual); 
      FreeTextProcessingResult result = nlpPipeline.processRequest(request); 
      List<KeyPhraseAnnotation> list = Arrays.asList(result.getDefaultView().getKeyPhraseAnnotations()); 
      kpMatrix.put(Long.toString(counter), list); 


       for (KeyPhraseAnnotation kp : list){ 
        System.out.println(kp.getValue() +" : " +kp.getImportance()); 

       } 
      //t += s_max+1; 
      current_num_docs++; 
      k = k + t; 
     } 
     System.out.println("this is u in end of loop: " + current_num_docs); 
    } 

    System.out.println("OUT OF FOR"); 
    /*while ((line = br.readLine()) != null && DocCounter < 50000) { 

     String[] parts = line.split("\\|"); 
     //String sessionId = parts[0]; 
     String memo_manual = parts[15]; 
     //String category = parts[2]; 

     //String AccountBalance = "2139"; 
     String manualMemoPrefix = "Caller/Customer Name:"; 
     //if (category.equals(AccountBalance) && memo_manual.toLowerCase().contains(manualMemoPrefix.toLowerCase())){ 
     if (memo_manual.toLowerCase().contains(manualMemoPrefix.toLowerCase())){ 
     DocCounter ++ ; 

     FreeTextProcessingRequest request = new FreeTextProcessingRequest(); 
     request.setText(memo_manual); 
     FreeTextProcessingResult result = nlpPipeline.processRequest(request); 
     List<KeyPhraseAnnotation> list = Arrays.asList(result.getDefaultView().getKeyPhraseAnnotations()); 
     kpMatrix.put(Long.toString(counter), list); 


      for (KeyPhraseAnnotation kp : list){ 
       System.out.println(kp.getValue() +" : " +kp.getImportance()); 

      } 
     } 
     counter++; 
    }*/ 
    br.close(); 

    } 
+0

你可以粘貼輸出 – 2014-09-05 11:22:55

回答

3
tempLine = raf.readLine().toString(); // first readLine 
    if (!tempLine.equals(raf.readLine().toString())) // second readLine 

每個readLine讀取一個新行,所以當然tempLine.equals(raf.readLine().toString())的將返回false(因爲你在比較兩個不同的線路)。只有兩條連續的線相等纔是真實的。

+0

沒有在系統輸出中的引號,所以它只是打印值 – 2014-09-05 11:24:54

+1

sysout不是問題的一部分。 sysout僅用作日誌記錄。閱讀sysout之上的兩行代碼,就會發生「魔術」。 – Korashen 2014-09-05 11:26:48

0
tempLine = raf.readLine().toString(); 
    if (!tempLine.equals(raf.readLine().toString())) 
    { 
     System.out.println("!tempLine.equals(raf.readLine().toString())"); 
    } 

raf.readLine()是一個函數,每次你調用它它讀取下一行。

+0

raf.seek(hit)如何將指針移動到行首?我的用例是我需要從文件中隨機讀取一行代碼。所以我隨機確定變量'hit'並且想要讀取'hit'位置的下一行文件。令人吃驚的是,rad.seek()正在走中間線而不是開始。你能建議嗎? – user3628777 2014-09-05 12:35:57