Forums Register Login

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

+Pie Number of slices to send: Send
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.
>
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks Jan, appreciated. :-)
I will have to do some reading on that and see how it could be implemented.
+Pie Number of slices to send: Send
 

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
The moustache of a titan! The ad of a flea:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1134 times.
Similar Threads
java.lang.NoClassDefFoundError
If I don't need simultanius threads of the same object?
count instances of each word in a String
Checking whether a string is a substring of another string without using indexOf method
What's wrong with the following code? Please suggest
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:46:46.