close

[Solved] ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 Error Occurs?

Today I am facing the following error ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 in Java.

How To Solve ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 Error ?

  1. How To Solve ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 Error ?

    To Solve ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 Error Indexes starts from 0. Your length is 3 and your counter variable (i) starts from 1.

  2. ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

    To Solve ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 Error Indexes starts from 0. Your length is 3 and your counter variable (i) starts from 1.

Solution 1

because array index start at 0

 public static void main(String[] args) {

Scanner input = new Scanner(System.in);
int n = 3;
int[] numbers = new int[n];
float total = 0;

for (int i = 0; i <= 2; i++) {
int row=i+1;
    System.out.println("Please type the number " + row + ":");
    numbers[i] = input.nextInt();

    total = total + numbers[i];

}

System.out.println("The average of the 3 number is: " + total / n);
}

Solution 2

Indexes starts from 0. Your length is 3 and your counter variable (i) starts from 1.

You can use

for (int i = 0; i < 3; i++)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read