I am a beginner and already broke my head with this elementary task! Help)

public class MInOfTwo { public static void main(String[] args) { int [] myArray = {1,4,3}; int [] myArray1 = {2,5,7}; int i=0; for (i=0; i<3; i++); { if (myArray[i]<myArray1[i]) System.out.println(myArray[i]); else System.out.println(myArray1[i]); } } } 

As a result, it gives out

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at MInOfTwo.main (MInOfTwo.java:10)

referring to the line with if .

    1 answer 1

    Remove the semicolon after the cycle.

    Immediately after for (i=0; i<3; i++) you have a semicolon, that is, the block following the cycle is no longer the body of the cycle , but simply a block that will be executed after the cycle. After executing the loop, i will be equal to 3 and after that you want to access myArray[i] and myArray1[i] and quite rightly get an ArrayIndexOutOfBoundsException .

    • one
      Thank you very much! Even ashamed of such nonsense! - GenykS