what am i doing wrong?
You have defined sieve()
but you never call it. You need to call it before printing:
public static void main(String[] args)
{
sieve(); // <<== Here
for (int i=0; i<count; i++)
{
if (isPrime[i])
{
System.out.println(i + " ");
}
}
}
The reason it worked in the book was that the initialization has been done in the constructor. Your modified code moved the initialization into a static function, but it fails to call that initialization.
You also need to remove parameters from the declaration of sieve
, because it already has access to isPrime
and count
.
An alternative is to remove the sieve
method altogether, replacing it with a static initializer. Instead of
public static sieve()
{
// Code to initialize isPrime
}
write
static
{
// Code to initialize isPrime
}
This change makes sieve()
method a static initializer, which is always called before anything else in your class gets to execute.