First off, the sample data both breaks your rules:
Quote:
You will read the text of the email from the keyboard. It ... contains only lowercase letters of the English alphabet and spaces.
'I' is uppercase
And it contains more than seven words:
thanks for the list of shopping Is helpful thanks
1 2 3 4 5 6 7 8 9
And a regex is a pretty inefficient way to do counting: I'd use the
Java String indexOf() Method[
^] or
What is the StringUtils.indexOfAny method in Java?[
^] method to locate the separators and work it out from there instead of playing with regex or creating new strings.