Array Index Out Of Bounds and Problem in Assigning Values
Code:
public class Ch6Exercise8 {
public static void main(String[] args) {
int[] divisors = new int[0];
int number = 7,
divisibilityCount = 0;
for (int x = 1; x <= number; x++) {
if (number % x == 0) {
divisibilityCount++;
divisors = new int[divisibilityCount];
}
for (int i = 0; i <= divisors.length - 1; i++) {
if (divisors[i] == 0) {
divisors[i] = x;
}
else {
divisors[i + 1] = x;
}
}
}
for (int x = 0; x <= divisors.length - 1; x++) {
System.out.println(divisors[x]);
}
}
}
can figure it out why is it out of bounds...
please help me here..
what i want in this program is that i want to determine if a number is a prime number(im okay with this part),
but im having problem with the ff:
1.) store it in an integer[] array
2.) the length of an array WILL ONLY DEPEND on the number of the values.
ex: (ofcourse prime numbers are only divisible by 1 and itself) ,
so i want to STORE those values in an array.
but as the case have said, THE LENGTH OF THE ARRAY WILL ONLY
DEPEND O N THE number (i.e Length) of the values. :confused: