2014-09-05 100 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

+0

这里'hit'不是行的数字,而是你想要的,但是文件中的字节数,它通常不在行首。 试试这个:'raf.seek(hit); raf.readLine(); //从击中到结束读取一行的一部分; String theLineYouWant = raf.readLine(); //读下一行,这应该是一个正常的,未删节的行;'。当然,你必须考虑那些在'hit'位置之后没有线的情况。 – ekaerovets 2014-09-05 14:20:27