简体   繁体   English

在文本文件中查找单词并计算特定单词

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

now I'll try to explain what I need to do. 现在,我将尝试解释我需要做什么。 I have file.txt file, It looks like: 我有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

So I have program's code: 所以我有程序的代码:

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

This program counting specific word (entered by keyboard) in file.txt. 该程序计算file.txt中的特定单词(由键盘输入)。

I need to make this program: for ex.: if I enter word: One It must show: 我需要制作这个程序:例如:如果输入单词: One它必须显示:

Word One repeated 3 times by John, Elisa, Albert

All what I need to elect by who this word repeated. 我需要根据这个词的重复者来选择所有。 But I don't know really how to make It, maybe LinkedList or I dont know, someone could help me? 但是我真的不知道如何制作它,也许是LinkedList还是我不知道,有人可以帮助我吗?

Thank you very much. 非常感谢你。

You can have a List<String> that holds where your word was repeated, and add elements to it on the fly. 您可以拥有一个List<String>来保存单词的重复位置,并动态添加元素。

To add elements to it you can use an extra variable lastName (of type String ), and in your while loop, do something like: 要向其中添加元素,可以使用一个额外的变量lastName (类型为String ),并在while循环中执行以下操作:

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

The first line if for "resetting" the lastName variable, after you changes the name of the name (assuming there is an empty line after all the words of each name) 第一行,如果要“重置” lastName变量,则在更改名称后(假设每个名称的所有单词后面都有一个空行)
The second line is setting it back to the new name after an empty line. 第二行将其设置为空行后重新设置为新名称。

And in addition when you increase counter do: 另外,当您增加counter时,请执行以下操作:

myList.add(lastName)

So the loop will be something like that: 因此,循环将是这样的:

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
}

I'd suggest you to use Map<String, Set<String>> where key is a word and value is the list of people names that "typed" this word. 我建议您使用Map<String, Set<String>> ,其中key是一个单词,value是“键入”该单词的人物姓名列表。

So, here is how you can define your collection: 因此,这是定义集合的方法:

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

Here is how you can add words there: 您可以在此处添加单词:

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

And here is how you can retrieve the value: 这是获取值的方法:

word2people.get(word)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM