Harshita Sethi
Harshita Sethi

Reputation: 2125

Count the no. of occurrence of the exact word in a file using java

I have requirement in which I have to find the no. of times a particular word appears in a file. For eg.

String str = "Hi hello how are you. hell and heaven. hell, gjh, hello,sdnc ";

Now in this string I want to count no. of times the word "hell" appeared. The count should include "hell" , "hell," all these words but not "hello". So according to the given string I want the count to be 2.

I used following approaches

1st:

int match = StringUtils.countMatches(str, "hell");

StringUtils is of org.apache.commons.lang3 library

2nd:

int count = 0;
Pattern p = Pattern.compile("hell");
                Matcher m = p.matcher(str);
                while (m.find()) {
                    count++;
                }

3rd

int count =0;
String[] s = str.split(" ");
for(String word: s)
if(word.equals("hell")
count++;

the 1st two approaches gave 4 as answer and the 3rd approach gave 1 as answer.

Please suggest anyway in which I can get 2 as answer and fullfill my requirement.

Upvotes: 4

Views: 1448

Answers (3)

ksraj98
ksraj98

Reputation: 350

Give this a try

String str = "put the string to be searched here";
Scanner sc = new Scanner(str);
String search = "put the string you are searching here";
int counter = 0; //this will count the number of occurences
while (sc.hasNext())
{
if (sc.next() == search)
counter++;
}

Since sc.next() reads complete next token it will hell and hello will not trouble you.

Upvotes: 1

08Dc91wk
08Dc91wk

Reputation: 4318

You can use a regular expression with the "\\b" word boundaries as follows:

  int matches = 0;  
  Matcher matcher = Pattern.compile("\\bhell\\b", Pattern.CASE_SENSITIVE).matcher(str);
  while (matcher.find()) matches++;

Upvotes: 5

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

You should use word boundary matchers in regex:

Pattern.compile("\\bhell\\b");

Upvotes: 5

Related Questions