lightning_missile
lightning_missile

Reputation: 2992

Word occurrence method not counting

I have a method that should count the occurrence of a word in a sentence and return the results. For some reason it's not counting. The code always returns 0.

Here's the code.

public static int countOccurance (String word, String sentence) {
int count = 0;
for (int i = 0; i != sentence.length()-word.length()-2; i++) {
    if (sentence.substring(i, i+word.length()-1).equalsIgnoreCase(word)) {
        count++;
    }
}
return count;
}

Can anyone tell me what's wrong with my method?

Upvotes: 1

Views: 139

Answers (7)

Rudi Kershaw
Rudi Kershaw

Reputation: 12972

Because others have already covered why your current code does not work, I would like to use this answer to make a recommendation. I would personally recommend using Java's regex API for this problem. Using something like the following;

public static int countOccurances(String regex, String sentence) {
    Pattern p = Pattern.compile(word);
    Matcher m = p.matcher(sentence);
    int count = 0;
    while(m.find()) count++;
    
    return count;
}

I hope this helps.

Upvotes: 0

Adi
Adi

Reputation: 2394

Using the Collections.frequency() method on a List of words sidesteps the whole need for keeping track of the counter yourself:

 public static int countOccurance (String word, String sentence) {
     List<String> words = Arrays.asList(sentence.split(" "));
     return Collections.frequency(words,word);
 }

Upvotes: 0

cyril wirth
cyril wirth

Reputation: 84

You can try this code :

public static void main(String[] args){
    String sentence = "Hello truc, Hello machi, and Hello all !";
    final String word = "hello";

    System.out.println(count(sentence.toLowerCase(), word.toLowerCase()));
}

public static int count(String sentence, String word){
    int cpt = 0;
    while(sentence.contains(word)){
        cpt++;
        sentence = sentence.substring(sentence.indexOf(word) + word.length());
    }
    return cpt;
}

Upvotes: 0

Nathan Norman
Nathan Norman

Reputation: 145

I just took your code and deleted your -1 and -2, since I didn't understand why they were there.

public static int countOccurance (String word, String sentence) {
    int count = 0;
    for (int i = 0; i != sentence.length()-word.length(); i++) {
        if (sentence.substring(i, i+word.length()).equalsIgnoreCase(word)) {
            count++;
        }
    }
    return count;
}

Tried this.

String sentence = "yakkity yak yak yak attack";
String word = "yak";

int wc = countOccurance (word, sentence);

System.out.println("The string \""+sentence+"\" contains the word "+word+" "+wc+" times.");

Result:

The string "yakkity yak yak yak attack" contains the word yak 4 times.

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

I would use regex

    Matcher m= Pattern.compile("\\b" + word + "\\b").matcher(sentence);
    while(m.find()) {
        count++;
    }

Upvotes: 0

user3974856
user3974856

Reputation:

Best is to check for the " " space,,, as each space to be encounter... count++ ..... and you can also check for the '\n' in the check case And make the loop to end at \0 string null character!!

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201439

Your sub-string is off, and your loop position check is incorrect too. When I change your method to,

public static int countOccurance(String word, String sentence) {
    int count = 0;
    for (int i = 0; i + word.length() < sentence.length(); i++) {
        if (sentence.substring(i, i + word.length()).equalsIgnoreCase(
                word)) {
            count++;
        }
    }
    return count;
}

public static void main(String[] args) {
    System.out.println(countOccurance("Hello", "hello hello world"));
    System.out.println(countOccurance("Fi", "Fee Fi Fum"));
}

it outputs the expected

2
1

Upvotes: 1

Related Questions