I'm trying to get all prime numbers between 1 and 10 and then putting them into an array. I get the ArrayIndexOutOfBoundsException
error.
Am I getting this because the first for loop has starNum <=10
and the array technically doesn't have any values yet?
I am not sure how else to phrase the statement while still have the range 1 through 10. Help is greatly appreciated.
public static void main(String[] args) {
int [] array = new int[args.length];
for (int starNum = 1; starNum <= 10; starNum ++){
boolean isPrime = true;
int list = Integer.parseInt(args[starNum]);
for (int i = 1; i*i <= starNum; i++) {
if (starNum % 2 == 0) {
isPrime = false;
}
}
if (isPrime) {
System.out.println(starNum);
array[starNum] = list;
System.out.println(array);
}
}
}