views:

107

answers:

3

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

+1  A: 

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
Your answer is right, but it's probably best not to give a direct answer to a "homework" problem.
Chris Jester-Young
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
Sorry, I still got the issuejava.lang.NullPointerExceptioneven if I try wordCount.get(seq)+1I guess the problem is that it doesn´t increment :(
Edgar
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
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,
Edgar
A: 

Alternative solution that uses Multiset from Google Collections.

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

for (String word : words.elementSet())
    System.out.println(word + ": " + words.count(word));
Chris Jester-Young
+1  A: 

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
Cletus you saved my daythe result is accurateACU value: 4ACY Value: 2AER Value: 3...Which is correct, Thank you very much!! :)
Edgar