My Matcher doesn't work. Why?

I tried to do this Codecademy’s challenge called “Determine the number of substrings that meet specific criteria. Part 2”. I attempted to accomplish the task by employing Pattern and Matcher objects, but it doesn’t work. Could you please tell me why?

    public static int findNumValues(String text, String findText) {
        int count = 0;
        Pattern pattern = Pattern.compile(text.toLowerCase());
        Matcher matcher = pattern.matcher(findText.toLowerCase());
        while (matcher.find()) count++;
        return count;
    }

I’m again going to suggest that you might benefit from reviewing the documentation for the classes you’re attempting to use, for both Pattern and Matcher

Let’s assume that you call that method like so: findNumValues("This is an example string!", "is");

The Pattern you’re attempting to match will be This is an example string!, which does not exist within the string is so you get 0 matches.

If we call findNumValues("is", "This is an example string!"); instead, you’ll get 2 matches.

3 Likes

Thank you! You’re right