0

I am getting the Array index out of bounds in this code cant find where i am getting wrong

    public static void main(String[] args) {
        // your code goes here
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){

            String s=sc.next();

            int count=0;

            char[] str=s.toCharArray();

            for(int i=0;i<str.length;i++){

                if(str[i]=='1' && str[i+ 1]!='1'){
                    count++;
                }
            }

            System.out.println(count);

        }

    }
}

error :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

What's wrong with this !!

  • 1
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Hülya Mar 05 '21 at 13:02
  • What is max value of `i` in your loop? Since condition is `i – Pshemo Mar 05 '21 at 13:13

0 Answers0