java字符串比较有趣的bug

时间:2014-09-05 11:19:36

标签: java string randomaccessfile

我有一个有趣的错误,程序点击这一行

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

每次循环中的随机索引。 不确定tempLine怎么可能不等于raf.readLine()。toString(),其中前一个赋值始终是apllied。 另一个有趣的(不确定它是否相关)是在某些时候raf.readLine()和raf.readLine()。toString()有两个不同的值。 急于求助: - )

   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(); 

    }

2 个答案:

答案 0 :(得分:3)

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

每个readLine读取一个新行,因此当然tempLine.equals(raf.readLine().toString())将返回false(因为您正在比较两个不同的行)。只有两条连续的线相等才会出现这种情况。

答案 1 :(得分:0)

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

raf.readLine()是一个函数,每次调用它都会读取下一行。