tags:

views:

141

answers:

2

Hi friends, My input file will be look like this

போதும்  1
போதும்  2
போதும்  3
போதும்  4
போதும்  5
போதும்  6
போதும்  7
போதும்  8
போதும்  9
போதும்  10
போதும்  11
போட்டால்    1
போட்டால்    2
பொன்    1
பொன்    2
பொன்    3
பொன்    4
பொன்    5

and my output want to be as

போதும்  11
போட்டால்    2
பொன்    5

How to select the each word with its maximum value using java program. Pls suggest me any ideas. Thanks in advance.

+11  A: 

Something along these lines:

  • Create a HashMap<String, Integer>
  • For each line in your input:
    • Split it into the word and number, and parse the number part (as Matthew comments below, using Scanner can help with this part)
    • See if there's anything in your map for that word
      • If there isn't, use map.put(word, value)
      • If there is, compare the current value with the new one, and replace the current value if the new one is higher
  • When you've read everything in the file, your map will contain (word / maximum value) pairs. You can iterate over the map's entries to get at everything you need.
Jon Skeet
The sample just shows sorted data sets, so in this special case we could even skip the test, if the stored value is bigger then the actual one. It will always be bigger.
Andreas_D
[Scanner.next](http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/Scanner.html#next%28%29), [hasNext](http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/Scanner.html#hasNext%28%29), and [nextInt](http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/Scanner.html#nextInt%28%29) are convenient for the parsing.
Matthew Flaschen
@Andreas_D: Yes, but I thought it better not to rely on that.
Jon Skeet
A: 

Check this program: http://leepoint.net/notes-java/data/collections/maps/ex-wordfreq.html It could be helpful to you.