簡體   English   中英

在文本文件中查找單詞並計算特定單詞

[英]Find word in text file and count specific words

現在,我將嘗試解釋我需要做什么。 我有file.txt文件,它看起來像:

John //first line - name
One
Three
Four

Peter //first line - name
Two
Three

Elisa //first line - name
One
Three

Albert //first line - name
One
Three
Four

Nicole //first line - name
Two
Four

所以我有程序的代碼:

 public class Testing {


        public static void main(String args[]) throws Exception {
            Scanner input = new Scanner(System.in);
            System.out.println("Select word from list:");
            System.out.println();

            try {
                FileReader fr = new FileReader("src/lt/kvk/i3_2/test/List.txt"); // this is list of words, everything all right here
                BufferedReader br = new BufferedReader(fr);
                String s;
                while((s = br.readLine()) != null) {
                    System.out.println(s);
                }
                fr.close();
                String stilius = input.nextLine();   // eneter word which I want to count in File.txt
                BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/test/File.txt")); // from this file I need to count word which I entered before

                int counter = 0;                
                String line;

                System.out.println("Looking for information");
                while (( line = bf.readLine()) != null){
                    int indexfound = line.indexOf(stilius);
                    if (indexfound > -1) {
                         counter++;
                    }

                }
                if (counter > 0) {
                    System.out.println("Word are repeated "+ counter + "times");}
                    else {
                    System.out.println("Error...");
                }
                bf.close(); 

            }
            catch (IOException e) {
                System.out.println("Error:" + e.toString());
                }
            }
        }

該程序計算file.txt中的特定單詞(由鍵盤輸入)。

我需要制作這個程序:例如:如果輸入單詞: One它必須顯示:

Word One repeated 3 times by John, Elisa, Albert

我需要根據這個詞的重復者來選擇所有。 但是我真的不知道如何制作它,也許是LinkedList還是我不知道,有人可以幫助我嗎?

非常感謝你。

您可以擁有一個List<String>來保存單詞的重復位置,並動態添加元素。

要向其中添加元素,可以使用一個額外的變量lastName (類型為String ),並在while循環中執行以下操作:

if (line.trim().length() == 0) lastName = null; 
else if (lastName == null) lastName = line;

第一行,如果要“重置” lastName變量,則在更改名稱后(假設每個名稱的所有單詞后面都有一個空行)
第二行將其設置為空行后重新設置為新名稱。

另外,當您增加counter時,請執行以下操作:

myList.add(lastName)

因此,循環將是這樣的:

List<String> resultList = new ArrayList<String>();
String lastName = null;
while (( line = bf.readLine()) != null){
   if (line.trim().length() == 0) lastName = null;
   else if (lastName == null) lastName = line;
   int indexfound = line.indexOf(stilius);
   if (indexfound > -1) {
      counter++;
      resultList.add(lastName);
   }
//do something with resultList, which contains the names
}

我建議您使用Map<String, Set<String>> ,其中key是一個單詞,value是“鍵入”該單詞的人物姓名列表。

因此,這是定義集合的方法:

Map<String, Set<String>> word2people = HashMap>();`

您可以在此處添加單詞:

String name = ...
String word = ...
Set<String> people = word2people.get(word);
if (people == null) {
    people = new HashSet<String>();
    word2people.put(people);
}
people.add(name);

這是獲取值的方法:

word2people.get(word)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM