I am suppose to use methods in order to count number of words in the a sentence. I wrote this code and I am not quite sure why it doesn\'t work. No matter what I write, I on
Please check for boundary conditions at str.charAt(i+1)
. It can result in a StringIndexOutOfBoundsException
when the string is terminated by a space.
I know you do not want an alternate method but string.split(" ").length() is an easier way to start.