Sieve of Eratosthenes malfunction
<p>I am trying to create a Sieve of Eratosthenes in Java but the code I wrote seems to be flawed.
I was wondering if anyone else can spot my mistake since I cannot. The output I get is simply [2], which means that my main loop doesn't work. I have only just started Java so I would appreciate it if you could give a detailed answer.</p>

<p>My code: </p>

<pre><code>public static int[] primes(int n)
{
    //Variable assignement
    double sqrt;
    List&lt;Integer&gt; primes= new ArrayList&lt;Integer&gt;();
    //Adds 2 to primes so i don't need to include the even numbers in my for loop.
    if(n&gt;1)
    {
        primes.add(2);
    }
    //For loop that goes through the oneven numbers up to n
    for(int counter1=3;counter1&lt;=n;counter1+=2)
    {
        sqrt=Math.floor(Math.sqrt(0.0+counter1));
        //for loop that tests if the first for loops number is prime
        for(int counter2=0;sqrt&lt;=primes.get(counter2);counter2++)
        {
            if(counter1 % primes.get(counter2) != 0 &amp;&amp; counter2 ==sqrt)
            {
                primes.add(counter1);
            }

            if(counter1 % primes.get(counter2)==0)
            {
                break;
            }
        }
    }
    return convertIntegers(primes);
}
//Converts the list to an array
public static int[] convertIntegers(List&lt;Integer&gt; integers)
{
    int[] ret = new int[integers.size()];
    for (int i=0; i &lt; ret.length; i++)
    {
        ret[i] = integers.get(i).intValue();
    }
    return ret;
}
</code></pre>
