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.
Post moved to the General Programming subforum. Please post programming help topics there in the future.
What is line 38? I don't really know what (/how) it's doing right now. Can you give some better names to a, b, c, d, e, f, and g? Razz
Line 38 is the arraycopy (added a comment in the code). And I used a through g because I couldn't think of what else to call them. Basically they're just tracking where in the 9x9 array I'm looking; a and b are every third row and column, respectively (since I'm trying to compare each 3x3 grid). c and d are the starting position of the array copy (row, column, respectively). And actually, I can replace d with b since the arraycopy will track the column for me. e is my counter for where in the 1x9 test array the arraycopy is dumping the elements. f and g are for comparing the elements of the test array; if there are any duplicates, the 3x3 grid has a duplicate, making the solution invalid.
You could rename then since you basically just gave them names Wink
But not as simple/short names, and the operators next to the variables explains what they're for (since it's a simple program).
"sudoku" is an int[][] (array of int[]), "test" is an int[] (array of int). You're asking System.arraycopy to store int[] values into an array of int, hence the ArrayStoreException.

Unrelated complaint: why is "z" a double when it's only used to hold integers?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement