1
package myMethods;

import java.util.Scanner;

public class CalculationsOnArrays {

  public static void main(String args[]) {

  Scanner input = new Scanner(System.in); 

  int x ;

  System.out.println("How many numbers are in the array");

  int N = input.nextInt();

  int[] arr = new int [N] ;


  System.out.println("Enter the numbers") ; 

    for (x = 0; x < N; x++) {

      arr[x] = input.nextInt();

   } 

    double average = 0 ;

    int count_negative_elements = 0;

    // calling method

    AVER(arr, average, count_negative_elements, N, x);

    input.close();

  }

  static void AVER(int[] arr, double average, int count_negative_numbers, int N, int x) // passing parameters to the method

      {

   int sum = 0, count = 0, negative_numbers = 0 ;

   sum += arr[x] ;

   average = sum / N;

System.out.println("The average of the numbers is " + average);

  if (arr[x] < 0) {

      count++ ; 

   negative_numbers = arr[x] ;

System.out.println("The negative numbers in the array are " + count);

System.out.println("They are " + negative_numbers) ;

    }

  }
 

}

I wanted to do some calculations on the 1D array.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Which line does the error happen on? What is the value of the array index when it goes wrong? Why did it have that value? – Andrew Morton May 02 '23 at 17:17
  • `AVER(arr, average, count_negative_elements, N, x);` <-- What is trhe value of `x` here? – Michael May 02 '23 at 17:18
  • To make it easier for others to reas your code, use proper indentation, and remove most of the empty lines. – pts May 02 '23 at 20:32

3 Answers3

2
for (x = 0; x < N; x++) {

      arr[x] = input.nextInt();

   } 

After this code x has value of N. so the Exception raises.

1

The ArrayIndexOutOfBounds exception seems to be on this line

sum += arr[x] ;

If N is 5, x can only go from 0 to 4. Seems like you are trying to access arr[5] which is out of bound.

justnisar
  • 528
  • 4
  • 10
1

You declare x, let it be incremented until it is equal to the length of the array, then pass it into a function where you use it to index the array again, but now the number inside it is greater than any valid index of the array. So it blows up with an IndexOutOfBoundsException.

So the immediate problem is you didn't have a reasonable value in this variable when you used it to index the array. But the big picture is: it's time to learn about controlling complexity because you are well on the way to creating things that are too hard for you to reason about.

Your best tools for controlling complexity are 1) limiting the scope of your variables and 2) writing cohesive methods, that means methods that do only one well-defined thing.

Your variable x has no good reason to be defined outside of the for loop, you can change it to look like this so that it is defined only inside of the loop:

for (int x = 0; x < N; x++) {
    arr[x] = input.nextInt();
} 

This way x has one and only one job, when x has done its job you don't have to worry about it anymore, you are rid of it.

There is no reason to pass x in to the AVER method that I can see. You can use arr.length to find the number of elements in arr. If you do need another variable in the AVER method, make one that is local to AVER. You're not saving anything by passing x around, you're just causing yourself trouble.

Change AVER to do the following:

  1. pass in as parameters only the array you want the average of, and nothing else,

  2. return the result of averaging the values of the array, instead of returning void (this makes it clear what are the inputs and what is the output), and

  3. remove all the stuff that has nothing to do with calculating an average and put it elsewhere, like in a separate static method that counts negative numbers (and does nothing else).

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276