-2

And I have to check if 2 or more numbers one after other are equal.
for example: 523(44)62(1111)14563(222)5

The code below shows this error:

java.lang.ArrayIndexOutOfBoundsException: 20

The code:

import java.util.Random;

public class Ushtr3 {
        public static void main(String [] args){

            int[] a = new int[20];
            Random nr = new Random();

            System.out.println("Program:");

            for(int i=0; i<a.length; i++){
                a[i]=1+nr.nextInt(6);
            }

            for(int i=0; i<a.length; i++){
                System.out.print(a[i]);
            }

            System.out.print("\n");

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

                while(i!=19){
                    if((a[i]==a[i+1]) && (a[i]==a[i+2]) && (a[i]==a[i+3]) && (a[i]==a[i+4])){
                        System.out.print("("+a[i]+""+a[i+1]+""+a[i+2]+""+a[i+3]+""+a[i+4]+")");
                        i++;
                        break;
                    }
                    else if((a[i]==a[i+1]) && (a[i]==a[i+2]) && (a[i]==a[i+3])){
                        System.out.print("("+a[i]+""+a[i+1]+""+a[i+2]+""+a[i+3]+")");
                        i++;
                        break;
                    }
                    else if((a[i]==a[i+1])&& (a[i]==a[i+2])){
                        System.out.print("("+a[i]+""+a[i+1]+""+a[i+2]+")");
                        i++;
                        break;
                    }
                    else if(a[i]==a[i+1]){
                        System.out.print("("+a[i]+""+a[i+1]+")");
                        i++;
                        break;
                    }
                    else {
                        System.out.print(a[i]);
                    }
                    i++;
                }

            }
            System.out.print(a[19]);

        }

    }

Can anyone show me if there is another way to do it ? thanks :)

Lae
  • 588
  • 1
  • 7
  • 26
Endra
  • 1
  • 4
  • The code runs fine for me. Are you sure that this is all? Did you remove some parts? – Lae Jan 13 '18 at 23:47
  • Sometimes it runs fine even for me but sometimes it shows the error . No i didn't , thank you for your editting – Endra Jan 13 '18 at 23:49
  • 1
    At a first glance ou will incur in the error if the 20th and the 19th digits are equals. You will try to read a[20] and get the out of bound error. – Eineki Jan 13 '18 at 23:58
  • It only runs into the exception sometimes because `&&` causes Java to stop checking after the first expression evaluates to false. Since the numbers are random, this sometimes prevents the "bad" array indices from being accessed. – O.O.Balance Jan 13 '18 at 23:59
  • Not quite a duplicate. Sure, the exception is nothing new, but the question "Can anyone show me if there is another way to do it ?" deserves an answer imho. – O.O.Balance Jan 14 '18 at 00:07

1 Answers1

1

You are getting the exception because you are letting i take values up to 18, then do this: if((a[i]==a[i+1]) && (a[i]==a[i+2]) && (a[i]==a[i+3]) && (a[i]==a[i+4])) which can result in an array index greater than 19 on a 20-element array.

Furthermore your code seems designed to only find runs of up to 5 numbers. There is a much simpler way to do it:

int length = 1; // the length of the current run
int number = a[0]; // the number currently being repeated
for(int i = 1; i < a.length; i++) {
    if(a[i] == number)
        length++;
    else {
        if(length > 1) {
            System.out.print("(");
            for(int j = 0; j < length; j++)
                System.out.print(number);
            System.out.print(")");
        } else {
            System.out.print(number);
        }
        number = a[i];
        length = 1;
    }
}
if(length > 1) { // last run
   System.out.print("(");
   for(int j = 0; j < length; j++)
       System.out.print(number);
   System.out.println(")");
} else {
    System.out.println(number);
}
O.O.Balance
  • 2,507
  • 4
  • 20
  • 34