Error trying to scan an array

Asked

Viewed 90 times

0

I am trying to scan the array within the loop, as per the code below:

import java.util.Scanner;

public class RapidoPora {


    public static void main(String[] args) {

        int[] nota = new int[10];



        for(int c = 0; c < 11; c++) {


            System.out.println(nota[c]);

        }




}


}

But this exception occurs:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
        at RapidoPora.main(RapidoPora.java:15)
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0

What can it be?

2 answers

3

If the vector size is 10 positions, Voce only has 10 positions to go through, not 11 as it is in your loop. Change to:

int[] nota = new int[10];


 for(int c = 0; c < 10; c++) {


     System.out.println(nota[c]);

 }

See it working on ideone: https://ideone.com/SFfEAa

  • 1

    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahh thanks bird

  • 1

    @Programmer If the answer helped you, you can accept it by clicking on v, so you close the question and brand that found the solution :)

-2

The error refers to the burst of the vector’s Dice. The right thing would be:

for(int c = 0; c < 10; c++) {

            System.out.println(nota[c]);

        }
  • 1

    I had already answered that, look at the other answer posted 5 hours ago.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.