2016-02-05 23 views
0

我試圖打開兩個文本文件並在其中找到類似的行。 我的代碼正確讀取了兩個文本文件中的所有行。 我已經使用了嵌套的for循環來比較第一個文本文件的line1與第二個文本文件的所有行等等。 然而,它僅檢測具有相同行號類似的路線, (例如線的txt1 1是cc cc cc和線的txt2 1是cc cc cc,那麼它正確地找到並打印出來), 但它不檢測同一這些文件中的行號不同。如何在兩個文本文件中查找類似的行,而不考慮它們出現的行號

import java.io.*; 
import java.util.*; 

public class FeatureSelection500 { 

    public static void main(String[] args) throws FileNotFoundException, IOException { 
     // TODO code application logic here 

     File f1 = new File("E://implementation1/practise/ComUpdatusPS.exe.hex-04-ngrams-Freq.txt"); 
     File f2 = new File("E://implementation1/practise/top-300features.txt"); 

     Scanner scan1 = new Scanner(f1); 

     Scanner scan2 = new Scanner(f2); 

     int i = 1; 
     List<String> txtFileOne = new ArrayList<String>(); 

     List<String> txtFileTwo = new ArrayList<String>(); 

     while (scan1.hasNext()) { 
      txtFileOne.add(scan1.nextLine()); 
     } 

     while (scan2.hasNext()) 

     { 

      txtFileTwo.add(scan2.nextLine()); 
     } 
    /* 
    for(String ot : txtFileTwo) 
    { 
    for (String outPut : txtFileOne) 

    { 
     // if (txtFileTwo.contains(outPut)) 
     if(outPut.equals(ot)) 
     { 

      System.out.print(i + " "); 

      System.out.println(outPut); 
      i++; 


     } 

    } 
    } 
*/ 

     for (int j = 0; j < txtFileTwo.size(); j++) { 

      String fsl = txtFileTwo.get(j); 
      // System.out.println(fileContentSingleLine); 

      for (int z = 0; z < 600; z++)         // z < txtFileOne.size() 
      { 
       String s = txtFileOne.get(z); 
       // System.out.println(fsl+"\t \t"+ s); 
       if (fsl.equals(s)) { 
        System.out.println(fsl + "\t \t" + s); 
        // my line 

        // System.out.println(fsl); 
       } else { 
        continue; 
       } 
      } 
     } 
    } 
} 
+0

兩個循環運作良好,但我沒有得到什麼不順心的,如果僅檢測在同一行號,而不是類似的文字聲明我希望它的方式是 –

+0

請修復您的格式。這是很難理解的。 – BalinKingOfMoria

+0

下降繼續; – StealthSpoder

回答

1

我做你的代碼看起來更好,歡迎您:) 反正我不明白,你得到的錯誤。它貫穿所有列表2在列表1每行的......

import java.io.*; 
import java.util.*; 
public class FeatureSelection500 { 

    public static void main(String[] args) throws FileNotFoundException, IOException { 
    // TODO code application logic here 

    File file1 = new File("E://implementation1/practise/ComUpdatusPS.exe.hex-04-ngrams-Freq.txt"); 
    File file2 = new File("E://implementation1/practise/top-300features.txt"); 

    Scanner scan1 = new Scanner(file1); 

    Scanner scan2 = new Scanner(file2); 

    List<String> txtFile1 = new ArrayList<String>(); 

    List<String> txtFile2 = new ArrayList<String>(); 

    while (scan1.hasNext()) { 
     txtFile1.add(scan1.nextLine()); 
    } 

    while (scan2.hasNext()) { 
     txtFile2.add(scan2.nextLine()); 
    } 
    for (int i = 0; i < txtFile2.size(); i++) { 
     String lineI = txtFile2.get(i); 
     // System.out.println(fileContentSingleLine); 
     for (int j = 0; j < txtFile1.size(); j++){ // z < txtFileOne.size(
      String lineJ = txtFile1.get(j); 
      // System.out.println(fsl+"\t \t"+ s); 
      if (lineI.equals(lineJ)) { 
       System.out.println(lineI + "\t \t" + lineJ); 
       // my line 

       // System.out.println(fsl); 
      } 
     } 

    } 
    } 
} 
+0

非常感謝orjan sunnerhagen,我甚至無法弄清楚問題所在。然而它昨天工作正確不知道如何:P。 M真的很好奇,想早點發現是什麼造成了問題。 –

+0

非常感謝您的幫助 –

1

我沒有看到您的代碼有任何問題。即使你評論的街區也絕對沒問題。因爲,你在做equals()你應該確保你在兩個文件中有相同的文本(相同大小寫),以便它們能夠成功地滿足條件。

for(String ot : txtFileTwo) 
{ 
    for (String outPut : txtFileOne) 
    { 
     if(outPut.equals(ot)) /* Check Here */ 
     { 
      /* Please note that here i will not give you line number, 
       it will just tell you the number of matches in the two files */ 
      System.out.print(i + " "); 
      System.out.println(outPut); 
      i++; 
     } 
    } 
} 
+0

好友感謝您的評論,但我的這裏不會是一個行號,不是一個問題,所以不是一個問題 –

+0

如果你想要的行號,那麼你將有將兩個for循環更改爲'for(int i = 0; i user2004685

相關問題