- Java ArrayStoreException error
- 22 Jan 2013 05:34:17 pm
- Last edited by Beta7 on 22 Jan 2013 09:37:50 pm; edited 1 time in total
Code:
public class SudokuVerifier {
public static void main(String[] args) {
int[][] sudoku = new int[9][9];
int[] test = new int[9];
int e, x = 0, y = 0;
double z = 0;
//input from file, send to sudoku[][]
for (int row=0; row<9; row++) {
for (int col=0; col<9; col++) {
for (int a=col + 1; a<9; a++) {
if(sudoku[row][col]==sudoku[row][a]) {
z = 1;
y = row;
break; } // end if
} } } // end for(a), for(col), for(row)
for (int col=0; col<9; col++) {
for (int row=0; row<9; row++) {
for (int a=row + 1; a<9; a++) {
if(sudoku[row][col]==sudoku[a][col]) {
z = 2;
y = col;
break; } // end if
} } } // end for(a), for(col), for(row)
for (int a=1; a<8; a+=3) {
for (int b=1; b<8; b+=3) {
e = 0;
for (int c=a; c<a+3; c++) {
System.arraycopy(sudoku, c, test, e, 3); // Line 38
e+=3; }
for (int f=0; f<9; f++) {
for (int g=f+1; g<9; g++) {
if(test[f]==test[g]) {
z = 3;
y = a;
x = b;
break; }
} } // end for(f), for(g)
} } // end for(a), for(b)
if(z==0) {
System.out.println("Valid Solution"); }
if(z==1) {
System.out.println("Row" + y + "has a duplicate number."); }
if(z==2) {
System.out.println("Column" + y + "has a duplicate number."); }
if(z==3) {
System.out.println("3x3 grid at (" + y + "," + x + ") has a duplicate number."); }
} } // end main, class
When I try to run this, I get the following error:
Code:
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at cse.SudokuVerifier.main(SudokuVerifier.java:38)
The error shows up when "an attempt has been made to store the wrong type of object into an array of objects." How is copying array elements an attempt to store the wrong type of object? I'm copying a 3x3 array into a 1x9 array, but I'm copying a single row (1x3 elements) at a time.