0

I'm trying to save numbers to an array through Scanner Input but
"java.lang.ArrayIndexOutOfBoundsException" keeps popping up.

Here is my code:

for (int counter = 1;1==1;counter++) {
            int bucky[] = new int[counter];
            int scan = oof.nextInt();
            bucky[counter] = scan;
            if (counter == 5) {
                System.out.println(bucky);
            }

Can someone explain what I'm doing wrong or where I'm going wrong in my thought process?

Zoe
  • 27,060
  • 21
  • 118
  • 148
DudeManGuy
  • 123
  • 2
  • 12
  • What is the first index of an array? Assuming its length is 1, what is its last index? https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – JB Nizet Aug 12 '18 at 07:47
  • in additiion to previous comment: 1) move array creation out of the for-loop. 2) Why not use `ArrayList` instead of array? – Max Farsikov Aug 12 '18 at 08:13

1 Answers1

0

Try

for (int counter = 1;1==1;counter++) {
            int bucky[] = new int[counter];
            int scan = oof.nextInt();
            bucky[counter-1] = scan;
            if (counter == 5) {
                System.out.println(bucky);
            }

One array of 5 positions {0,1,2,3,4}

Your Array have 1 position {0}

bucky[counter-1] = scan;