1

I asked a question about counting the number of times a word is in ArrayList:

ACU ACU ACU ACU ACY ACY AER AER AER AGC

So for
ACU we will get 4,
ACY we will get 2,
AER we will get 3,
AGC we will get 1.

I got some help but I cannot make it work.
Darren gave me a very important answer:

Map<String, Integer>wordCount = new HashMap<String, int>();
  for(String seq : yourWordList){
     wordCount.put(seq, wordCount.get(seq++));
  }

But in the part wordCount.put(seq, wordCount.get(seq++)); I get an error that cannot convert from String to int, I tried to modify the code to work but I'm getting incorrect numbers

ACU 0 ACU 1 ACU 1 ACU 1 ACY 1 ACY 2 AER 2 AER 3 AER 3

int value=0;
Map<String, Integer>wordCount = new HashMap<String, Integer>();
for(String seq : WordList){
  Set<String> set = wordCount.keySet();
  value = set.size();
  wordCount.put(seq, value));
}

Please help me on this one. Thanks to all.

Community
  • 1
  • 1
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
  • 1
    @Edgar - Whether or not this is "homework", you are not going to learn if you simply cut-and-paste code provided in SO answers. You should try to code and debug your own stuff. – Stephen C Feb 04 '10 at 04:45
  • Thanks for that advice, I tried to debug my self, the problem here was that I am not familiar using maps and hash, sorry if this seems to be copy paste, but i really tried to run that, even I modified that code my own, thats why I ask for your expert comments. Greetings – edgarmtze Feb 04 '10 at 06:09

3 Answers3

3

What you want is:

Map<String, Integer>wordCount = new HashMap<String, Integer>();
for (String seq : yourWordList) {
  Integer count = wordCount.get(seq);
  wordCount.put(seq, count == null ? 1 : count + 1);
}

The Map is from String to Integer. Integer is immutable so you can't increment it in place. You have to retrieve the current value, increment it and put it back. You're trying to call wordCount.get(seq++), which doesn't make a lot of sense. get() has to be passed in a String (for this kind of Map) and you can't use ++ on an immutable Integer anyway.

cletus
  • 616,129
  • 168
  • 910
  • 942
  • 1
    Cletus you saved my day the result is accurate ACU value: 4 ACY Value: 2 AER Value: 3... Which is correct, Thank you very much!! :) – edgarmtze Feb 04 '10 at 04:37
2

wordCount.get(seq++) is probably the issue--try incrementing the return value of the get method (right now you're apparently attempting to increment the String argument, triggering the error. Try changing that to wordCount.get(seq)+1

Justin Searls
  • 4,789
  • 4
  • 45
  • 56
  • Your answer is right, but it's probably best not to give a direct answer to a "homework" problem. – C. K. Young Feb 04 '10 at 04:26
  • I agree with Justin. The second code snippet seems to be completely off-mark. Fix the first code snippet with Justin's suggestion. – Tahir Akhtar Feb 04 '10 at 04:29
  • Sorry, I still got the issue java.lang.NullPointerException even if I try wordCount.get(seq)+1 I guess the problem is that it doesn´t increment :( – edgarmtze Feb 04 '10 at 04:33
  • The NullPointerException was likely caused by the fact that each word `seq` had never been initialized to zero in the map. Therefore, each of those items would have evaluated to `null+1` at runtime. (Merely an FYI, as you have your cut-and-paste homework answer provided above at this point.) – Justin Searls Feb 04 '10 at 05:32
  • thanks for your comments, as I said, I didnt mean this to be cut and paste, I really tried to run the code and modify it myself. Thanks for your time, Greetings, – edgarmtze Feb 04 '10 at 06:11
2

Alternative solution that uses Multiset from Guava.

Multiset<String> words = HashMultiset.create();
for (String word : wordList)
    words.add(word);

for (String word : words.elementSet())
    System.out.println(word + ": " + words.count(word));
C. K. Young
  • 219,335
  • 46
  • 382
  • 435