-1

I am getting an exception and i don´t understand why

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 = 1; i <= 3; i++) {

        System.out.println("Please type the number " + i + ":");
        numbers[i] = input.nextInt();

        total = total + numbers[i];

    }


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

Console:

Please type the number 1:
3
Please type the number 2:
4
Please type the number 3:
5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    at Ejercicio12.main(Ejercicio12.java:17)
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
im_cool
  • 27
  • 1
  • 1
  • 5
  • 1
    Java array indexes are numbered from from 0, and the last index being n-1. In your case numbers[3] => [0,1,2]. You have started your loop from 1 and going to =3. It should be for (int i = 0; i < 3; i++) – Sushim Mukul Dutta Feb 09 '19 at 20:15
  • 3
    arrays start with index `0` and go up to `length-1` – Sharon Ben Asher Feb 09 '19 at 20:15
  • 3
    Possible duplicate of [Why is array indexing in Java start with 0?](https://stackoverflow.com/questions/24841172/why-is-array-indexing-in-java-start-with-0) – Andrey Tyukin Feb 09 '19 at 20:20
  • Obligatory Dijkstra reference: [Why numbering should start at zero](http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html) – Andrey Tyukin Feb 09 '19 at 20:28
  • What if I get the same exception but my numbering starts at 0? for(int i=0; i<=food.size(); i++ ){ System.out.println(food.get(i)); } –  Jan 05 '23 at 08:36

4 Answers4

0

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);
}
AMA
  • 74
  • 4
  • a trivial question is better answered with comment – Sharon Ben Asher Feb 09 '19 at 20:21
  • 1
    @SharonBenAsher No, please, don't answer in comments - comments are for commenting. However, I'd appreciate if you'd contribute your voice for closing this question as a duplicate - in 10 years of SO, there really should be a duplicate for a "java arrays are 0-based" question. – Andrey Tyukin Feb 09 '19 at 20:23
  • @AndreyTyukin, I voted for closing. however, if there was no duplicate, I believe it is common practice here to answer trivial questions in comments – Sharon Ben Asher Feb 09 '19 at 20:27
  • @SharonBenAsher *If* there is a way to close the question (e.g. as "trivial typo" / "problem resolved in a way that is unlikely to help anyone else" etc.), then, yes, feel free to leave a comment, sure, why not. I agree that in this particular case, a comment would have been sufficient. – Andrey Tyukin Feb 09 '19 at 20:33
0

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++)
Coderino Javarino
  • 2,819
  • 4
  • 21
  • 43
0

Try this, insert this inside your main method

Scanner input = new Scanner(System.in);

    int n = 3;
    int[] numbers = new int[n];
    float total = 0;


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

        System.out.println("Please type the number " + (i + 1) + ":");
        numbers[i] = input.nextInt();

        total = total + numbers[i];

    }


    System.out.println("The average of the 3 number is: " + total / n);
  • 1
    Instead of posting slightly modified code explain what was wrong in the original code and what you changed to make it work (keep in mind what has already been posted in other answers). Currently this answer doesn't provide any value to the thread. – Cray Feb 21 '20 at 11:42
0

If you're facing error listed below

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

var arr = arrayOf("One","Two","Three")
 println(arr[3])

In this Array we just defined three values and index start from 0, So Index 3 doesn't exist in array that's why it's giving this Exception

Try to print index value which includes in Array Such As

println(arr[1])
Rehan Khan
  • 1,031
  • 13
  • 10