The substring begins with the character at the specified index and extends to the end of this string.
Your if
condition is only true once, if the last character of the string is a space. Perhaps you wanted charAt
? (And even this won't properly handle double spaces; splitting on whitespace might be a better option.)