0

I am trying to make practice application by getting 10 numbers from users and adding them to an array. here is my code

public static void getNumber(){
        for(int i=0; i<10; i++){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter number " + i );
        int NumbersArray[] = {};
        int no1 = scanner.nextInt();
        NumbersArray[i] = no1;
        System.out.println(NumbersArray);
        }

The problem is that after accepting first value it gives an error message saying

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Number.getNumber(Number.java:17)
    at Number.main(Number.java:8)

Line number 8 as per my format is

getNumber();

And Line number 17 as per my format is

NumbersArray[i] = no1;

Please help

Rishit Shah
  • 355
  • 5
  • 20

5 Answers5

7

In this line:

int NumbersArray[] = {};

You have explicitly initialized it to an empty array; it's equivalent to new int[0]. You cannot store anything in such an array. Storing something doesn't automatically allocate the memory in the array for that element.

You must size the array appropriately before you use it, because Java arrays aren't resizable.

int[] numbersArray = new int[10];

Although int NumbersArray[] is completely legal, by convention the brackets go on the datatype, and the first letter of the variable name is lowercase.

Additionally, call Arrays.toString to print your array properly.

rgettman
  • 176,041
  • 30
  • 275
  • 357
2

The error explains the problem: you are trying to access an array at an index that is out of bounds. At line 17, you are doing the following on the first iteration where i=0:

NumbersArray[0] = no1;

However, NumbersArray was instantiated as such:

int NumbersArray[] = {};

The above means that you are creating an empty array with no space allocated to it. Instead, consider initializing some space:

int[] numbersArray = new int[10];

Or more commonly, consider using an ArrayList and add your elements to it:

List<Integer> numbers = new ArrayList<>();

for(int i=0; i<10; i++){
    ...
    numbers.add(no1);
}
David Yee
  • 3,515
  • 25
  • 45
2

Your problem is that you intend to asign de int value as the array variable.

First of all you don't need to create new Scanner object in each iteration.

Scanner scanner = new Scanner(System.in);

You need to declare an int array outside the loop and tell him the it's length is 10:

int[] numbersArray = new Int[10];

You shouldn't use:

System.out.println(NumbersArray);

Because it prints the reference identifier of it array, but not it content, so you must be use this to it:

for(int i=0; i < numbersArray.length; i++){
    System.out.println(numbersArray[i]);
}

The final code is the next:

int[] numbersArray = new Int[10];

Scanner scanner = new Scanner(System.in);

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


    System.out.println("Enter number " + i );

    int number = scanner.nextInt();

    numbersArray[i] = number;
}

System.out.println("The numbers are:" );

for(int i=0; i < numbersArray.length; i++){
    System.out.println(numbersArray[i]);
}
garciparedes
  • 1,749
  • 2
  • 18
  • 34
1

You are allocating an array of size 0.

Also, you are allocating the array 10 times, move it outside the loop.

Additionally, you are creating a new Scanner instance 10 times, you only need to create it once.

public static void getNumber(){
    int size = 10;
    int NumbersArray[] = new int[size];
    Scanner scanner = new Scanner(System.in);
    for(int i=0; i<size; i++){
        System.out.println("Enter number " + i );
        int no1 = scanner.nextInt();
        NumbersArray[i] = no1;
        System.out.println(NumbersArray);
    }
}
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
0

David Yee and rgettman have perfectly answered your question. I only wish to show you how to add 10 numbers without using any array in the first place.

public static void getNumber(){
       Scanner scanner = new Scanner(System.in);
       int no1=0;
       for(int i=0; i<3; i++){
           System.out.println("Enter number " + (i+1) );
           no1 += scanner.nextInt();
       }
       System.out.println("Sum = "+no1);
   }
Somnath Rakshit
  • 555
  • 1
  • 6
  • 17
  • Thank you for the answer. I however wanted to store them in one place and not add them. That is why i used array. However for addition i will keep this in mind. – Rishit Shah Oct 14 '15 at 23:36