2014-03-24 162 views
0

我們在課堂上編寫了這段代碼,我無法理解/操作它。我想對其進行修改,以便用戶輸入,而不是搜索整個文件,並在文件中打印出單詞的位置。txt文件中的搜索字符串

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 
import java.util.StringTokenizer; 
import java.util.Map.Entry; 

public class Assingment7 { 

    public Map<String, Integer> getWordCount(String fileName){ 

     FileInputStream fis = null; 
     DataInputStream dis = null; 
     BufferedReader br = null; 
     Map<String, Integer> wordMap = new HashMap<String, Integer>(); 
     try { 
      fis = new FileInputStream(fileName); 
      dis = new DataInputStream(fis); 
      br = new BufferedReader(new InputStreamReader(dis)); 
      String line = null; 
      while((line = br.readLine()) != null){ 
       StringTokenizer st = new StringTokenizer(line, " "); 
       while(st.hasMoreTokens()){ 
        String tmp = st.nextToken().toLowerCase(); 
        if(wordMap.containsKey(tmp)){ 
         wordMap.put(tmp, wordMap.get(tmp)+1); 
        } else { 
         wordMap.put(tmp, 1); 
        } 
       } 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally{ 
      try{if(br != null) br.close();}catch(Exception ex){} 
     } 
     return wordMap; 
    } 

    public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){ 

     Set<Entry<String, Integer>> set = wordMap.entrySet(); 
     List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set); 
     Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() 
     { 
      public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) 
      { 
       return (o2.getValue()).compareTo(o1.getValue()); 
      } 
     }); 
     return list; 
    } 

    public static void main(String a[]){ 
     Assingment7 mdc = new Assingment7(); 
     Map<String, Integer> wordMap = mdc.getWordCount("C://fox.txt"); 
     List<Entry<String, Integer>> list = mdc.sortByValue(wordMap); 
     for(Map.Entry<String, Integer> entry:list){ 
      System.out.println(entry.getKey()+" ==== "+entry.getValue()); 
     } 
    } 
} 

有什麼想法?

+0

你想要的行號,單詞號碼,段落號碼,或任何其他具體的有關單詞的位置? – Signus

+0

我想打印出使用該單詞的行 – Cullen

+0

不需要DataInputStream。使用Java 7試用資源的好例子,你只需要一個FileReader和一個BufferedReader。無論如何,除了逐行讀取文件的方法之外,不需要其他任何東西,它可以更改爲在找到該單詞時返回帶有行號的int(如果找不到該單詞,則返回-1) )。 –

回答

0

你可以改變你的主要方法是這樣的 - 用戶在控制檯輸入他們想要搜索的字符串,並且如果在地圖中找到該鍵,它將輸出與其相關的值。

public static void main(String a[]) { 
    Assingment7 mdc = new Assingment7(); 
    Map<String, Integer> wordMap = mdc.getWordCount("C://fox.txt"); 
    List<Entry<String, Integer>> list = mdc.sortByValue(wordMap); 

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    String input = null; 

    try { 
     input = br.readLine(); 
    } catch (IOException e) { 
     System.out.println(e.getMessage()); 
    } 

    for(Map.Entry<String, Integer> entry:list) { 
     if(entry.getKey().equalsIgnoreCase(input)){ 
      System.out.println(entry.getKey() + " ==== " + entry.getValue()); 
     } 
    } 
} 
+0

我該如何改變它以輸出使用用戶輸入單詞的行? – Cullen

+0

那麼文件中可能出現了很多單詞,但是您可以更改Map對象,以便每個輸入都有一個字符串和整數,並且它還包含每個單詞被找到的行號? – maxdh