arrays - I keep getting java.lang.ArrayIndexOutOfBoundsException: 500 -
i keep getting error array out of bounds , can't figure out wrong.
int[] oddarray = new int[500];//holds odd numbers int[] primearray = new int[500];//holds prime numbers int[] modarray = new int[500];//holds mod values int remainder, p = 0, x = 0; //fills oddarray & modarray for(int n = 0; n < 500; n++) { oddarray[n] = (n * 2) + 1; modarray[n] = (n* 2) + 1; } for(int = 0; < 500; i++) { //finds prime numbers for(int n = 0 ; n < 500; n++) { //divides odd numbers current mod value remainder = oddarray[n] % modarray[x]; //if remainder not 0 place value in prime array if(remainder != 0) { primearray[p] = oddarray[n]; p++; } } //prints out list of odds/mod/and primes side side system.out.println(oddarray[i] + " | " + modarray[i] + " | " + primearray[p]); ++x; }
this error code
1 | 1 | 0
3 | 3 | 0
exception in thread "main" java.lang.arrayindexoutofboundsexception: 500 @ projectprime_v1.projectprime_v1.main(projectprime_v1.java:41) java result: 1 build successful (total time: 0 seconds)
you never resetting p
, exceed 499, , cause exception. make sure inside loop write p = 0;
reset it. same goes x
hope helps!
Comments
Post a Comment