+ 2

Can anyone solve my error in this code...

Here is my code that is not working.....plzzz give any suggestion https://code.sololearn.com/cGHnIUrkYahP/?ref=app

4th Jun 2018, 3:39 PM
Harsh Agrawal
Harsh Agrawal - avatar
8 ответов
+ 1
class str { public static void main(String args[]) { String s= "She was feeding the little rabbit with an apple"; s=s.toUpperCase(); int i,l,c=0; char ch,ch1; l= s.length(); for(i=0;i<l-1;i++) { ch= s.charAt(i); ch1= s.charAt(i+1); if(ch==ch1) c++; } System.out.println(c); } }
4th Jun 2018, 4:29 PM
Matthias
Matthias - avatar
+ 2
In line 13 you have ch1= s.charAt(i+l); that points to a char outside of the string since l is equal to the string length.
4th Jun 2018, 4:00 PM
Louis
Louis - avatar
+ 1
You should only loop until l-1, because you are using i+1 inside the loop As an additional note: don't use l as a variable, since l and I (small l and large i) look the same in some fonts
4th Jun 2018, 3:57 PM
Matthias
Matthias - avatar
+ 1
Thats the online line which needs a fix: instead of for(i=0;i<l;i++) use for(i=0;i<l-1;i++)
4th Jun 2018, 4:17 PM
Matthias
Matthias - avatar
+ 1
You also changed line 13 from i+1 to i+l, which was not necessary. Now it should work
4th Jun 2018, 4:30 PM
Matthias
Matthias - avatar
0
Louis and Matthias can u do it in my code n resend it to me n also i want to count and output the no of double letter sequences that exist in the string
4th Jun 2018, 4:05 PM
Harsh Agrawal
Harsh Agrawal - avatar
0
Matthias still it is not working u can do it in code n den resend it to me plzz
4th Jun 2018, 4:26 PM
Harsh Agrawal
Harsh Agrawal - avatar
0
Matthias thank u soo much but can u xplain me this code by commenting on each line that how it wll work!!
4th Jun 2018, 4:40 PM
Harsh Agrawal
Harsh Agrawal - avatar