title: Why is my algorithm not giving the expected output?
body: <p>I am trying to create an Java implementation of the <a href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">Sieve of Eratosthenes</a> algoritm.</p>
  
  <p>I have the following code, which runs, although gives an incorrect output.</p>
  
  <pre><code>import java.util.ArrayList;
  public class sieveOfEratosthenes {
      private static final ArrayList&lt;Integer&gt; test = new ArrayList&lt;&gt;();
      public static void main (String [] args) {
          java.util.Scanner tempInput = new java.util.Scanner(System.in);
          System.out.println("What number would you like the prime numbers to be generated to?");
          int maxPrime = tempInput.nextInt();
          for(int i = 2; i &lt;= maxPrime; i++) {
              test.add(i);
          }
          getPrimeList(maxPrime);
      }
  
      private static void getPrimeList(int maxNumber) {
          int sqrtOfNum = (int) Math.sqrt(maxNumber);
          int temp = 0, i = 0;
          int currentPrime = test.get(i);
          boolean completed = false;
          i++;
          //do {
          while((completed == false) &amp;&amp; (i &lt; test.size())) {
              if(i &gt;= test.size()) {
                  completed = true;
              } else if((temp &lt;= sqrtOfNum) ) {
                  removeMultiples(currentPrime);
              }
              i++;
              if (i &lt; test.size()) {
                  currentPrime = test.get(i);
              }
          }
          //}while(completed == false &amp;&amp; (i &lt; test.size()));
          System.out.println("Prime numbers upto: " + maxNumber + ": " + test);
      }
  
      private static void removeMultiples(int primeToTest) {
          ArrayList&lt;Integer&gt; temp = new ArrayList&lt;&gt;();
          for (Integer toTest : test) {
              if (!(((toTest) % primeToTest) == 0)) {
                  temp.add(toTest);
              }
          }
          test.clear();
          test.addAll(temp);
      }
  }
  </code></pre>
  
  <p>An example of the output given by the program is as follows:</p>
  
  <pre><code>What number would you like the prime numbers to be generated to?
  10
  Prime numbers upto: 10: [3, 5, 9]
  </code></pre>
  
  <p>Obviously the output for the above example should be:</p>
  
  <pre><code>Prime numbers upto: 10: [2, 3, 5, 7]
  </code></pre>