0

Im stuck on writing Word occurrence in a string. I got some tip(in task notes) to use is compareToIgnoreCase. so I tried something like this:

splitwords = StringCont.split("\\s");
for(int i=0; i<splitwords.length; i++)
{
    if(splitwords[1].compareToIgnoreCase(splitwords[i]) == 0)
        splitcount++;
}

It is of course just what I can do and probably bad way. When I run the code, I get sometimes out of array exeption and sometimes it runs. What is missing is: go through all words and check them and skip the words which were already counted. I will be happy to get any help with this so can move along and understand how it can be coded. Thank you :)

Edit: It seems I did not explain the problem enough clearly, but I get nice answer about the map object which easily put together what I needed. I did not know about map. So yea, I was trying to find the number of times every given word is found in the string.

tangens: it should mean-take the first word(where first whitespace is) splitwords[1] and compare it to all other words in string splitwords[i] and if it is 0(equals), then count++.

Esko: there indeed are white spaces like in sentence. But I still got this exeption. I dont know why thru.

3
  • 1
    What exactly should your method do? I don't get the comparison of splitwords[1] to splitwords[i]. What is it good for?
    – tangens
    Dec 20, 2009 at 20:43
  • are you trying to get the number of words in a string; or the number of times a given word is found in the string?
    – Bozho
    Dec 20, 2009 at 20:50
  • Not an answer, but just a tip: use equalsIgnoreCase instead of compareToIgnoreCase.
    – BalusC
    Dec 20, 2009 at 21:16

4 Answers 4

4

Store the words and their counts in a Map.

String[] words = string.toLowerCase().split("\\s+");
Map<String, Integer> wordCounts = new HashMap<String, Integer>();

for (String word : words) {
    Integer count = wordCounts.get(word);
    if (count == null) {
        count = 0;
    }
    wordCounts.put(word, count + 1);
}

Note that I called toLowerCase() before split() as you seem want to have case insensitivity.

2
  • or use apache-commons HashBag instead of the HashMap (+1)
    – Bozho
    Dec 20, 2009 at 21:22
  • Depending on the context, characters that are not whitespace may separate words (ex: "This_is.a_test"), in that case, change the split's pattern (\\s+) to ([^\\p{L}]+) - For those unfamiliar with java's regex pattern, that code translates to "any group of non-letter characters", and is replacing "any group of whitespace characters". --- Please note that the specific code will depend on what's considered a word. For example, if you want something like "up-voted" to be considered a single word, you will need to change the pattern, for the '-' character is not a letter, thus, splits. Jul 11, 2012 at 20:50
0

Are you looking for the occurrence of a certain word in sentence or the cumulated word count of the all the words in sentence? Based on your sample code I'd expect the former but your explanation makes me think of the latter.

In any case, something obvious: If there aren't any whitespaces in the input String, String#split() returns an array containing exactly one element - the original String itself. Since array indexes start from zero, that splitwords[1] is causing the ArrayIndexOutOfBoundsException because the only String available is in index [0].

0

You can use a Set and store each word into and at the end get the size of the set.

You will get the number of different word not the number of word of course.

2
  • With HashSet this is true, however with TreeSet... :)
    – Esko
    Dec 20, 2009 at 20:50
  • 1
    Sorry i didn`t get it : a TreeSet is a set so it will not have duplicate element, so the size will be the different word count. No ?
    – Patrick
    Dec 20, 2009 at 21:17
0

If you want to count word occurence, you should use a HashMap<String, Integer>. There you can store a counter for each word found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.