Compare characters in string in java?
<p>My task is to  removes the consecutively repeating characters and replaces the count of the repeated characters in the string. When I compared the two characters, I got the IndexOutOfBoundException. I dont know how to rectify it. Am beginner. I attached my code here. Could anyone verify my code and help me.</p>

<p>program:</p>

<pre><code>import java.util.*;
public class RemoveDupchar{

     public static void main(String []args){

        String string = "aabbccdefatafazaacddsegf";

   char[] chars = string.toCharArray();

   ArrayList&lt;Character&gt; al=new ArrayList&lt;Character&gt;();
     for(char c :  chars)
     {
         al.add(c);
     }

     int i=0;

     int j=0,k=1;
     char[] r = new char[100];

     for(i=0;i&lt;al.size();i++)
     {
         char ch;
         ch=al.get(i);
         if(ch!=al.get(i+1))      **//I got Runtime error here**
         {
             r[j++]=ch;
         }
         else
         {
             r[j++]=ch;
             while(al.get(i)==ch)
             {
                 ++k;
                 ++i;
             }
             r[j++]=(char)k;
         }
     }

     for(char c: r)
     {
         System.out.print(c);
     }
}
</code></pre>

<p>}</p>