I need to count the number of words and I am assuming the correct way to do it is by calculating the number of times that the previous character in a string is not a letter
The reason you are getting an IndexOutOfBoundsException is probably because when i is 0 your inner loop will have string.charAt(i-1) which will throw an exception since 0-1 is -1. If you fix that your method might work, although you can use more efficient techniques.