-3

I don't know why it is showing array index out of bound exception

import java.util.*;
public class longLong {
    public static void main(String []args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t != 0) {
            int n = sc.nextInt();
            int arr[] = new int[n];
            int sum = 0;
            int count = 1;
            int j = 0;

            for (int i = 0; i < n; i++){
                arr[i] = sc.nextInt();
                if (arr[i] < 0) {
                    sum = sum - arr[i];
                } else sum = sum + arr[i];
            }

            for (int i = 0; i < n; i++) {
                if (j < n) {
                    while (arr[j] <= 0) {
                        j = j + 1;
                    }
                }
                count = count + 1;
                j = j + 1;
            }
            System.out.println(sum + " " + count);
            t--;
        }
    }
}

I am trying to solve a question where a array of n size is given and we have to calculate sum of array and operations to be performed to make it positive array and the condition is we can invert only one sub array at a time

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 2
    Where is the actual error occurring? Have you stepped through the code or added logging to check your assumptions regarding the array indices? – Dave Newton Jun 21 '23 at 15:42
  • 1
    The `while(arr[j] <= 0) { j = j + 1; }` part seems very strange to me. Debug your code and pay special attention to this part of the code. The `ArrayIndexOutOfBoundException` is probably happening at the `while(arr[j] <= 0)` line. – Matheus Jun 21 '23 at 15:42
  • When you got the `ArrayIndexOutOfBoundException` error message, it should have included the size of the array, and the (invalid) value of the index. It should have included a stack trace, which would indicate the line in the source code that triggered the error. Questions of this type should include that information, along with a description of your attempt to find the error yourself. – Old Dog Programmer Jun 21 '23 at 15:46
  • In addition, it is a good idea to search for [similar questions](https://stackoverflow.com/search?q=%5Bjava%5D+ArrayIndexOutOfBoundsException) . If you still need to ask, be prepared to explain why answers to similar questions didn't help. – Old Dog Programmer Jun 21 '23 at 15:50
  • Take a look at this 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) . Do answers there help? – Old Dog Programmer Jun 21 '23 at 16:00

1 Answers1

0

The exception is occurring in the following loop.

for(int i=0; i<n; i++){
    if(j<n){
        while(arr[j]<=0){
            j=j+1;
        }
    }
    count=count+1;
    j=j+1;
}

Specifically, the following statement.

while(arr[j]<=0)

Since j is incremented at 2 locations, a subsequent resolve of arr[j] will throw an exception.

Here is an example input and output.

2
5
1 2 3 4 5
15 6
5
3 2 1 0 -1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
    at Example.main(Example.java:21)

As a note, the exception will provide the line-number on which the exception occurred at.

In this case, line 21.

at Example.main(Example.java:21)
Reilas
  • 3,297
  • 2
  • 4
  • 17