0

I have to check if a sequence of any numbers are equals. The user will submit a sequence, and if, the numbers repeat in sequence, he won some points.

And the sequence to win the points it's a sequence of three. For example: 1 3 4 4 4 5 He won the points because he inputted a sequence of 3 numbers 4.

The sequence of numbers it's on a Vector. The size of the vector, It's given by the user too.

for (int i = 0; i < M.length; i++) {
            if (M[i] == M[i + 1] && M[i + 1] == M[i+2]) {
                if (L[i] == L[i + 1] && L[i + 1] == L[i + 2]) {
                    ValuePoint = 0;
                } else {
                    PExtraM = i;
                    ValuePoint = 30;
                }

Scanner sc1 = new Scanner(System.in);

        R = sc1.nextInt();

        int M[] = new int[R];
        int L[] = new int[R];

        for (int i = 0; i < M.length; i++) {
            M[i] = sc1.nextInt();
        }

        for (int i = 0; i < L.length; i++) {
            L[i] = sc1.nextInt();
        }


//The problem It's here ************************************


        for (int i = 0; i < M.length; i++) {
            if (M[i] == M[i + 1] && M[i + 1] == M[i+2]) {
                if (L[i] == L[i + 1] && L[i + 1] == L[i + 2]) {
                    ValuePoint = 0;
                } else {
                    PExtraM = i;
                    ValuePoint = 30;
                }

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at maratona.Maratona2.main(Maratona2.java:37) Java Result: 1

  • You know that the largest value the variable "i" can have is M.length - 1, therefore you cannot access M[i + 1], and M[i + 2] since these indexes are outside of the array when i = M.length - 1. – emil Aug 17 '19 at 13:24
  • Why are you dividing your input into 2 arrays? – Joakim Danielson Aug 17 '19 at 13:44

3 Answers3

1

i < M.length

Now let's assume the length of the Vector you are saying is 5 ok? Now my loop will run till i is less than 5, right? Now go to your next code :

if (M[i] == M[i + 1] && M[i + 1] == M[i+2])

Let's take the value of i as

4 (suppose)

which is in fact less than 5 and the loop condition satisfies. But see the next code, it becomes

M[4]==M[5]&& ==M[6]

Obviously since the length of the given Vector is 5, my last element's index will be 4. So after that **5 & 6 ** will show null only. That's why it's saying ArrayIndexOutOfBounds Exception error at 5. Hope this helps!

1

your loop variable i must stop at m.length-3

   (i <m. length-2)

to have i+1=m.length-2 and i+2=m.length-1

but in your case you are trying to access i+1=m.length and i+2= m.length+1 both are out of bounds on the last two iterations

Bilal Rammal
  • 814
  • 8
  • 14
0

As the others already said, u r overshooting the boundaries of your array. You need to stop the loop 2 earlier to prevent.

You possably want to use something like that:

    int sequenceLength = 3;
    for (int i = 0; i <= M.length - sequenceLength; i++) {
        boolean correct = true;
        for (int j = 0; j < sequenceLength && (correct = (M[i] == M[j+i])); j++);

        if (correct){
            ValuePoint = 0;
        } else {
            PExtraM = i;
            ValuePoint = 30;
            break;
        }
    }
  • This solved the problem. I wanted to do that. Thank's for the help. The problem was in the size of the boundaries of the array. It was going into 6, but the size was 5. – Lucas Gomes Aug 17 '19 at 15:53