1

It only uses the prime numbers to check if the other numbers are also prime

static public int[] primeGen(int a){

    int[] series={2};

    if (a==1 || a==2 || a<=0){
        return series;
    }   

this is where the errors occur

    else{

        boolean Prime = false;

        for (int i = 3; i<=a; i++){

            boolean[] state = {};

            for (int j = 0; !(state[state.length-1]) && (j<series.length); j++){
                state = Arrays.copyOf(state, state.length +1);
                state[state.length -1] = i % series[j] ==0;
            }

            for (int k = 0; (Prime) && (k<state.length); k++){
                Prime = !(state[k]);
            }

            if (Prime){
                series = Arrays.copyOf(series, series.length +1);
                series[series.length -1] = i;
            }
        }
        return series;
    }
} 

Sorry if I just made a rookie mistake, cause I've been learning Java for 3 days now

  • 4
    What behavior do you expect and what do you currently get? – Jeffrey Bosboom Feb 16 '15 at 16:10
  • I expected an array of prime numbers that ends at the input number, and up until a couple of minutes ago, I just got some errors, but now It's working flawlessly. Even though this method alone doesn't give me an array that I can see (in stead i see this [I@15db9742), another method can use this array just fine for my purposes. – Nderim Xhemajli Feb 17 '15 at 21:35

3 Answers3

2

Your state array is initialized to an empty array, so !(state[state.length-1]) attempts to access an invalid index of the array (-1).

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Also since Prime is initialized to false, the second inner loop is skipped and the subsequent if test also is skipped. – user3745362 Feb 16 '15 at 16:18
0

This line

for (int k = 0; (Prime) && (k<state.length); k++){

means that the loop will only be executed if Prime is true. However you have initialized Prime to false and only set it true inside the loop.

It looks like you are using Arrays.copyOf to increase the size of your output array. You can just use Vector.add() instead and the JVM will take care of any resizing needed.

Neil Masson
  • 2,609
  • 1
  • 15
  • 23
0

Now it's working fine!

My mistake was that I didn't know that the for loop doesn't start at all if the statement in the middle for(int j =0; (Prime)&&(j<); j++){} is't true. What I thought was that this would be checked only at the end of each repetition. Anyway, I wouldn't have realized this without u guys pointing out that the statements don't add up, so thanks a lot!

        boolean Prime = false;

        for (int i = 3; i<=a; i++){

            boolean[] state = {};

            for (int j = 0; (j<series.length); j++){
                state = Arrays.copyOf(state, state.length +1);
                state[state.length -1] = i % series[j] == 0;
       //This is where I added my statement that breaks the loop
                if (!(state[state.length-1]==false&&j<series.length)){break;}
            }

            for (int k = 0; (k<state.length); k++){
                Prime = !(state[k]);
       //As well as here
                if (!(Prime==true&&k<state.length)){break;}
            }

            if (Prime == true){
                series = Arrays.copyOf(series, series.length +1);
                series[series.length -1] = i;
            }
        }
        return series;
    }
}