• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to restrict file writing to unique occurrence of a matched word that can occur multiple times?

 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,

Consider the following method that finds matching words in a file and writes them in another file.

Now, lets say the input file (is the file which i m scanning for matching words) has four occurrences of the word "Alice". The above method finds these four occurrences. What i would like to achieve now is how do i restrict the matched word writing into a file to only one time. What i mean is that the above method finds four occurrences of the word "Alice". Now i want the matched word Alice to be written only once in a file and also is there a way by which i could keep track of the counter. So that the output file should contain something like "Alice (found) 4 times..."

Any pointers or hints or suggestions are most welcome.
>
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could store the words in a Map<String, Integer> - the found word as the key and the counter as the value, then evaluate the map after your program ran.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jan, appreciated. :-)
I will have to do some reading on that and see how it could be implemented.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Thanks Jan, appreciated. :-)
I will have to do some reading on that and see how it could be implemented.

Jan's advice is exactly what I would have suggested, except that I would advise making it a Map<String, AtomicInteger>, because AtomicInteger's are mutable. With Integers, you'll have to do a lot of replacement.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic