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: 
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.
In your code you just need to add `break;` line just after `count++;` line.
Something like the following:

```
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++;
                break;
            }
        }
        System.out.println(comment);
    }
    System.out.println("count is " + count);
    return count;
}

```