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

时间:2012-11-11 12:13:05

标签: java file search text find

现在我将尝试解释我需要做什么。 我有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或者我不知道,有人可以帮助我吗?

非常感谢。

2 个答案:

答案 0 :(得分:0)

您可以拥有一个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
}

答案 1 :(得分:0)

我建议你使用Map<String, Set<String>>,其中key是一个单词,value是“键入”这个单词的人名列表。

因此,您可以通过以下方式定义集合:

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

以下是在那里添加单词的方法:

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)