Search code examples
javastringindexoutofbounds

Checking for consecutively repeated characters in java


I am quite new to java. I am wondering if it is possible to check for a certain number of consecutively repeated characters (the 'certain number' being determined by the user) in a string or an index in a string array. So far I have tried

int multiple_characters = 0;
String array1 [] = {"abc","aabc","xyyyxy"};
for (int index = 0; index < array1.length;i++){
   for (int i = 0;i<array1[index].length;i++){
      if (array1[index].charAt(i) == array1[index].charAt(i+1)){
         multiple_characters++;
      }
   }
}

But with this I get a StringIndexOutOfBounds error. I tried fixing this by putting in an extra if statement to make sure i was not equal to the array1[index].length, but this still threw up the same error. Other than the manual and cop-out method of:

if ((array1[index].charAt(i) == array1[index].charAt(i+1) && (array1[index].charAt(i) == array1[index].charAt(i+2))

and repeating however many times, (which would not be great for quick changes to my code), I can't seem to find a solution.


Solution

  • You are getting StringIndexOutOfBoundsException because you are trying to access string.charAt(i + 1) where i goes up to the highest index (i.e. string.length() - 1) of string.

    You can do it as follows:

    class Main {
        public static void main(String[] args) {
            int multiple_characters = 0;
            int i;
            String array1[] = { "abc", "aabc", "xyyyxy" };
            for (int index = 0; index < array1.length; index++) {
                System.out.println("String: " + array1[index]);
                for (i = 0; i < array1[index].length() - 1; i++) {
                    multiple_characters = 1;
                    while (array1[index].charAt(i) == array1[index].charAt(i + 1) && i < array1[index].length() - 1) {
                        multiple_characters++;
                        i++;
                    }
                    System.out.println(array1[index].charAt(i) + " has been repeated consecutively " + multiple_characters
                            + " time(s)");
                }
                if (multiple_characters == 1) {
                    System.out.println(array1[index].charAt(i) + " has been repeated consecutively 1 time(s)");
                }
                System.out.println("------------");
            }
        }
    }
    

    Output:

    String: abc
    a has been repeated consecutively 1 time(s)
    b has been repeated consecutively 1 time(s)
    c has been repeated consecutively 1 time(s)
    ------------
    String: aabc
    a has been repeated consecutively 2 time(s)
    b has been repeated consecutively 1 time(s)
    c has been repeated consecutively 1 time(s)
    ------------
    String: xyyyxy
    x has been repeated consecutively 1 time(s)
    y has been repeated consecutively 3 time(s)
    x has been repeated consecutively 1 time(s)
    y has been repeated consecutively 1 time(s)
    ------------