I'm trying to print a 1D array with 26 values into a table with 5 columns. This would result in 6 rows (5 rows of 5 values and 1 row of 1 value) but because there are no values in the 27th-30th, my for and while loops continue to throw an error.
I have the numbers in an ArrayList and can print from that if easier, but I run into the same issue.
My code:
int z = 0;
while (z < primeArray.length) {
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 5; col++) {
System.out.print(primeArray[z] + "\t");
z++;
} // end inner for
System.out.println();
} // end outer for
} // end while
**Here's my results: **
1 2 3 5 7
11 13 17 19 23
29 31 37 41 43
47 53 59 61 67
71 73 79 83 89
97 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 26 out of bounds for length 26
at PrimeNumbers.numberLoopTest(PrimeNumbers.java:87)
at PrimeNumbers.main(PrimeNumbers.java:21)
I know WHY I'm getting the error but I don't know how else I could print an array like that.