2010-08-01 112 views
0

我想從文本文件中讀取一行文本,並將每行放入一個映射,以便我可以刪除重複的單詞(例如測試測試)並打印出沒有重複單詞的行。我必須做錯事,因爲我基本上只有一行作爲我的關鍵字,而每行只讀一行。有什麼想法嗎?謝謝。Java + readline與BufferedReader

public DeleteDup(File f) throws IOException { 

    line = new HashMap<String, Integer>(); 
    try { 
     BufferedReader in = new BufferedReader(new FileReader(f)); 
     Integer lineCount = 0; 
     for (String s = null; (s = in.readLine()) != null;) { 
      line.put(s, lineCount); 
      lineCount++; 
      System.out.println("s: " + s); 
     } 
    } 
    catch(IOException e) { 
     e.printStackTrace(); 
    } 
    this.deleteDuplicates(line); 
} 
private Map<String, Integer> line; 
+0

當您將代碼粘貼到您的問題中時,請檢查它是否格式正確。原稿中的選項卡將壓痕縮小。 – JeremyP 2010-08-01 09:08:40

回答

3

說實話,你的問題還不是特別清楚 - 這不是顯而易見的,爲什麼你有lineCount,還是會做什麼deleteDuplicates,或者爲什麼你命名爲line變量的方法時,它不是實際上是一條線 - 它是從線條到該線條出現的最後一行號碼的地圖。

除非您需要行號,否則我會使用Set<String>

但是,除此之外,如果你看line之後的keySet,那麼將是所有的行。這是假設文本文件是真正在您的系統的默認編碼(這是什麼FileReader使用,不幸的是 - 我通常使用InputStreamReader並明確指定編碼)。

如果您可以給我們一個簡短的,但完整的程序,您使用的文本文件作爲輸入,預期的輸出和實際的輸出,這將是有益的。

1

你的問題不是很清楚。

但是,當經過您的代碼片段時,我認爲您嘗試刪除每行中的重複單詞。

以下代碼段可能會有幫助。

public class StackOverflow { 

    public static void main(String[] args) throws IOException { 
     List<Set<String>> unique = new ArrayList<Set<String>>(); 

     BufferedReader reader = new BufferedReader(
       new FileReader("C:\\temp\\testfile.txt")); 

     String line =null; 
     while((line = reader.readLine()) != null){ 

      String[] stringArr = line.split("\\s+"); 
      Set<String> strSet = new HashSet<String>(); 
      for(String tmpStr : stringArr){ 
       strSet.add(tmpStr); 
      } 
      unique.add(strSet); 
     }  
    } 
} 
0

我看到的代碼只有問題是DeleteDup沒有指定返回類型。否則代碼看起來不錯,並正確地從文件中讀取。

請發佈deleteDuplicates方法代碼和使用的文件。

1

我從你的問題中瞭解到打印行中沒有重複單詞的行。
可能你可以嘗試下面的代碼片段。

public void deleteDup(File f) 
    { 
     try 
     { 
      BufferedReader in = new BufferedReader(new FileReader(f)); 
      Integer wordCount = 0; 
      boolean isDuplicate = false; 
      String [] arr = null; 
      for (String line = null; (line = in.readLine()) != null;) 
      { 
       isDuplicate = false; 
       wordCount = 0; 
       wordMap.clear(); 

       arr = line.split("\\s+"); 
       for(String word : arr) 
       { 
        wordCount = wordMap.get(word); 
        if(null == wordCount) 
        { 
         wordCount = 1; 
        } 
        else 
        { 
         wordCount++; 
         isDuplicate = true; 
         break; 
        } 
        wordMap.put(word, wordCount); 
       } 
       if(!isDuplicate) 
       { 
        lines.add(line); 
       } 
      } 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    private Map<String, Integer> wordMap = new HashMap<String, Integer>(); 
    private List<String> lines = new ArrayList<String>(); 

在這個片段中,將包含沒有重複的話在它的線條。 這本來是更容易找到你的問題,如果我們知道

this.deleteDuplicates(line); 

嘗試這樣做。也許它沒有清除任何使用的數據結構。因此,前面幾行檢查過的單詞也會檢查其他行,儘管它們不存在。

0
  1. 您正在打印每行讀取,而不僅僅是獨特的行。
  2. 你的deleteDuplicateLines()方法不會做任何事情,因爲在HashMap中永遠不會有任何重複。

所以一點也不清楚你的實際問題是什麼。

相關問題