aspose file tools*
The moose likes Beginning Java and the fly likes How to restrict file writing to unique occurrence of a matched word that can occur multiple times? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to restrict file writing to unique occurrence of a matched word that can occur multiple times? " Watch "How to restrict file writing to unique occurrence of a matched word that can occur multiple times? " New topic
Author

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

Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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.
>


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 144

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.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Thanks Jan, appreciated. :-)
I will have to do some reading on that and see how it could be implemented.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to restrict file writing to unique occurrence of a matched word that can occur multiple times?