Java: Array Index Out of Bounds Exception
<pre><code>System.out.print("Enter an integer:  ");
Scanner sc = new Scanner(System.in);

int x = sc.nextInt();
int lArray = x - 2;
int[] newArray = new int[lArray];

System.out.println("Let's display all possible integers...");
for (int i = 0; i &lt;= newArray.length; i++) {
    newArray[i] = i + 2;
    System.out.print(newArray[i] + " ");
}
</code></pre>

<p>I've just started Java recently, but I sure that if I coded similarly in another language, I would face the same problem.  This is an excerpt from an application where it lists all the prime numbers up until the user's input.</p>

<p>The reason why x-2 is used as the definition of lArray is because the length of the array will be all the integers from 2 until the number {2, 3, 4, 5... x}.</p>

<p>I noticed that for the line</p>

<pre><code>for (int i = 0; i &lt;= newArray.length; i++) {
</code></pre>

<p>if I change <code>i &lt;= newArray</code> to <code>i &lt; newArray</code>, the code works without error.  However, the user's input, x, is left out which is a problem if x is prime.</p>
