I have written code for a magic square generator that takes an odd int(n). I used the standard rows = n-1 and columns = n/2. I am not sure why my code has an out of bounds exception. If anyone with a fresh pair of eyes could please point out my error it would be much appreciated.
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;
public class part1_2 {
public static void main(String[] args) {
System.out.println("input an odd value of n");
Scanner ns = new Scanner(System.in);
int n = ns.nextInt();
int ROWS = n;
int COLUMNS = n;
int[][] values = new int[ROWS][COLUMNS];
while (n % 2 == 0) {
System.out.println("n must be odd");
n = ns.nextInt();
}
int[] list = new int[n*n ];
for(int x = 1; x < (n*n+1); x++) {
list[x-1]=x;
}
int counter = 0;
for(int r = (n-1); r< n+1; r ++) {
for(int c = (n/2); c<(n+1); c++) {
values[r][c] = list[counter];
if(c+1 == n) {
c = 0;
}
counter++;
//}
//list.remove(counter);
//counter++;
//System.out.println();
//}
//System.out.println(values[1][1]);
}
if(r+1 == n) {
r = 0;
}
}
System.out.println(values);
}
}