Question: I have a List of Strings. For each string i wish to see if the first occurrence of the word "joe" is present. I am separating by white space as I do not wish to count the word "joey", for example.

My current code counts every occurrence of the word "joe", how do I edit it so it only counts the first occurrence of the word, and then moves onto the next string in the list. 

```
public int counter(List<String> comments) {
    int count = 0;
    String word = "joe";
    for (String comment : comments) {
        String a[] = comment.split(" ");

        for (int j = 0; j < a.length; j++) {

            if (word.equals(a[j])) {
                count++;

            }
        }
        System.out.println(comment);
    }
    System.out.println("count is " + count);
    return count;
}

```

EDIT

```
str.add("the hello my the name is joe the this joe is a test");
str.add("i was walking down joe then suddenly joe said hi");

```

I want my code to return 2 for this (joe has appeared in each String)

Answer: 
You can use regular expressions to check whether the entire String contains the word without the need to split it into individual words first.

A regular expression that matches the word "joe" but not "joey" would be the following:

```
\bjoe\b

```

The `\b` matches the bounds of a word, so the whole expression matches the start of a word, then the word, which must be joe and then the end of the word.

In Java this could be realized using the `matches(pattern)` method on a String:

```
"hello joe, how are you?".matches(".*\\bjoe\\b.*");

```

Note that the `matches` function requires the regular expression to match the entire string to return true, so we have to add `.*` at the start and the end, which will match any number of arbitrary characters. (The `.` matches an arbitrary character, the `*` signals that you want to match the preceding subexpression an arbitrary number of times)

This regular expression has the advantage, that it still works with punctuation. Just splitting on spaces would fail to recognize joe in the String "hello joe, how are you?"

To wrap it all up, this would be the entire solution:

```
public int countMatches(List<String> comments) {
    int numberOfMatches = 0;
    for (String comment : comments) {
        if (comment.matches(".*\\bjoe\\b.*")) {
            numberOfMatches++;
        }
    }
    return numberOfMatches;
}

```

If you want to match an arbitrary search term, you have to be careful, because some characters have a special meaning in regular expressions. I recommend using `Pattern.quote` (`import java.util.regex.Pattern;`):

```
String pattern = ".*\\b" + Pattern.quote(word) + "\\b.*";

```

Then you can match comments with `comment.matches(pattern)`.
A regular expression also works and makes this code a little shorter.

```
public int counter(List<String> comments) {
    String regex = "(.* )?joe( .*)?";
    return (int) comments.stream().filter(s -> s.matches(regex)).count();
}

```

**Edit:**

The regex of @Paelle is slightly better, use `.*\\bjoe\\b.*` instead.