I have to find number of consecutive capital letters in a string. For Example, "LITTLE RABBIT" Number of consecutive capital letters = 2 ( first is TT and second is BB) Program is not working.
The program is giving an error message as follows
java.lang.StringIndexOutOfBoundsException: String index out of range: 14
try {
String s = " LITTLE RABBIT";
s.toUpperCase();
int l = s.length();
int a = 0;
char ch1, ch2;
for (int i = 0; i < l; i++) {
ch1 = s.charAt(i);
ch2 = s.charAt(i + 1);
if (ch1 == ch2) {
a++;
}
}
System.out.println(a);
} catch (StringIndexOutOfBoundsException e) {
System.out.println(e);
}