java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
<p>I'm brand new to Java and my first assignment was to implement a "for" loop. I wrote this program in C++ and it compiles in Java, but I got an error at runtime. Can anyone tell me what's wrong?</p>

<pre><code>import java.util.Scanner;
import java.util.Vector;

public class GlobalMembersMain
{

    public static Vector&lt;Integer&gt; get_prime_factors(int number)
    {

        Vector&lt;Integer&gt; primefactors = new Vector&lt;Integer&gt;();
        for (int j = 2; j &lt;= number; j++)
        {
            if (number % j == 0)
            {
                primefactors.add(j);
                number = number / j;
                j = 1;
            }
        }
        return primefactors;
    }

    public static void main(String[] args)
    {
        int number;
        int count = 1;
        System.out.print("Enter integer to analyse:");
        System.out.print("\n");
        Scanner scan = new Scanner(System.in);
        number = scan.nextInt();
        Vector&lt;Integer&gt; primefactors = new Vector&lt;Integer&gt;();
        primefactors = get_prime_factors(number);
        System.out.print("Prime factors are ");
        for (int a = 0; a &lt; primefactors.size() + 1; a++)
        {
            if (primefactors.elementAt(a) == primefactors.elementAt(a+1))
            {
                count++;
            }
            else
            {
                System.out.print(primefactors.elementAt(a));
                System.out.print(" (");
                System.out.print(count);
                System.out.print(") ");
                count = 1;
            }
        }
        System.out.print("\n");
    }
}
</code></pre>

<p>The output:</p>

<pre><code>Enter integer to analyse:
10
Prime factors are 2 (1) Exception in thread "main" java.lang.ArrayIndexOutOfBoun
dsException: 2 &gt;= 2
        at java.util.Vector.elementAt(Unknown Source)
        at GlobalMembersMain.main(GlobalMembersMain.java:36)
</code></pre>
