generating prime numbers...using an array
I am trying to complete a class assignment and am currently unsuccessful...the assignment requires:
This program requires that you find all of the primes between 1 and some number that you allow the user to input.
Fill an array with the consecutive integers between 1 and the top number.
Identify 2 as the first prime and then remove all numbers that are evenly divisible by 2 (use modulus function).
Go to the next number remaining on the list (the next prime); now remove all of the numbers divisible by it.
Continue until you have only prime numbers in your list. When can you stop?
Now, write a Java program that generates all of the primes between 2 and some upper bound (greater than 50) that you allow the user to enter. As intermediate data, print one line for each divisor you eliminate along with the numbers eliminated. For example:
2: 4 6 8 10 12 14 16 18...
3: 9 15 21 27
so far I have the following code, but it isn't working...
Scanner keyboard = new Scanner(System.in);
int sentinel = 0;
int  numberArray;
System.out.println("Enter the size of the array from 1 to 100:");
size = keyboard.nextInt();
numberArray = new int[size];
//Fill the array.
for (int i=0; i<size; i++)
//Identify 2 as the first prime number and then remove all numbers that are evenly divisible by 2
int baseNumber = 2;
for (int i=1; i<size; i++)
if (numberArray[i] != sentinel)
int base = numberArray[i];
System.out.print(base + ": ");
if (numArray[i]%baseNumber == 0)
System.out.print(numberArray[i] + " ,");
numberArray[i] = sentinel;