Good day) Explain how to fix the error)

private double E; double Efficiency() { double[] firstEtalon = firstEtalon(); double [] secondEtalon = secondEtalon(); int n1 = 0,n2 = 0; for ( int i = 0; i < r1; i++ ) { for (int j = 0; j < r2; j++ ) { if ((firstEtalon[i] < r1 ) && (secondEtalon[j] > r2)){ n1++; } else if ((secondEtalon[j] < r2 )&& (firstEtalon[i] >r1)){ n2++; } } E = ((n1+n2)/160); System.out.println(E); } return E; 

Here is:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 80 at Laba.Distance.Efficiency(Distance.java:77) at Laba.Test.main(Test.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

Process finished with exit code 1

Thank you very much)

  • What are r1 and r2 equal to? - post_zeew
  • @post_zeew do you need numbers or how do I call them?) - Dasha Novikova
  • See the answer. - post_zeew

1 answer 1

In the expression firstEtalon[i] or secondEtalon[j] you go outside the array.

The value of r1 must be no more than firstEtalon.length , and the value of r2 must be no more than secondEtalon.length .

How to fix it depends on what you are doing in your program and what you want to receive.

You can replace r1 with firstEtalon.length and r2 with firstEtasecondEtalonlon.length , for example (this will correct the error, but may break the logic).

  • I only compare the number in the cell of the array with the desired number (r). What kind of array can there be? - Dasha Novikova
  • @DashaNovikova, Suppose you have an array on ten elements, when you call, for example, array[15] you will go beyond the array array and get an ArrayIndexOutOfBoundsException . - post_zeew 7:49