now I’ll try to explain what I need to do.
I have file.txt file, It looks like:
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.
I need to make this program: for ex.: if I enter word: One It must show:
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?
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.To add elements to it you can use an extra variable
lastName(of typeString), and in your while loop, do something like:The first line if for “resetting” the
lastNamevariable, after you changes the name of the name (assuming there is an empty line after all the words of each name)The second line is setting it back to the new name after an empty line.
And in addition when you increase
counterdo:So the loop will be something like that: