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