-1

`this has been bugging me .giving me error.....can anyone tell me how to solve that: exception in thread "main" StringIndexOutOfBoundsException: String index out of range: 8'


   public class Solution {
   public static boolean checkit(String input){
       int i,j;
       boolean s=false;
       for(i=0;i<=input.length()-3;i++){
           for(j=i+2;j<=input.length()-1;j++)
           {
               if(input.charAt(i)==input.charAt(j+1) && input.charAt(i+1)==input.charAt(j))
                   s=true;

           }
       }
     return s;  
   }
   public static void main(String [] args){

       System.out.print(checkit("ABcBAxxs"));

}
}

Manuel
  • 3,828
  • 6
  • 33
  • 48
  • 1
    on which line? it means you are trying to get the char at index 8, while the String has (max) 8 characters – Stultuske Apr 16 '20 at 07:27

1 Answers1

0

having

      for(j=i+2;j<=input.length()-1;j++)
      {
          if(input.charAt(i)==input.charAt(j+1) && input.charAt(i+1)==input.charAt(j))

when j values input.length()-1 then input.charAt(j+1) access at input.length() so out of your String


Out of that, when s become true it is useless to continue to loop because s cannot become false back. You can remove your variable s, replace s=true; by return true; and return s; by return false;

bruno
  • 32,421
  • 7
  • 25
  • 37
  • ok i get it now..... about your comment actually it was just a code for checking occurrence of BA for AB if present return true...its solved now.....thnks – The_Eternal_Wisp Apr 16 '20 at 07:37