-4

I know that the problem lies in the command line, but I have tried every which way to find a way to solve it, but I have completely no idea how I can fix the array to not be zero. I am still really new to arrays and have looked everywhere in my textbook on how to do this, but the only example does not include arrays.

here is the error I am getting.

I am using Netbean

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at arraysize.Arraysize.main(Arraysize.java:20)

Here is my code as well

package arraysize;



public class Arraysize {


public static void main(String[] args) {
    int[] array = new int[ 10 ];
    for ( int counter = 0; counter < array.length; counter++)
    array[counter] = Integer.parseInt(args[counter]);
   System.out.printf("%s%8s\n", "Index", "Value");

    for (int counter = 0; counter < array.length; counter++)
       System.out.printf("%5d%8d\n", counter, array[counter]);


}

}
henna994
  • 13
  • 4

1 Answers1

0

It is because your input args array length is less than length of array, in the moment you iterate to 10 in row array[counter] = Integer.parseInt(args[counter]); and in moment counter variable exceed args length you get exception, for solve this you need to pass args array with 10 or more length. To checkargs array argument you may pass it's size to console:

public static void main(String[] args) {
         System.out.printf("arguments array size is: %d", args.length); 
         //your code
}
fxrbfg
  • 1,756
  • 1
  • 11
  • 17