java - Looking for duplicate values in a 2 dimensional array -


the assignment working on requires create sudoku game without using classes, methods, encapsulation, etc. new java bare me. having trouble validating values user inputs either "fourarray" or "ninearray" contains no duplicate values. far have been trying use nested for-loop in order iterate through both columns , rows of either array. example, have been trying include following piece of code @ end of program determine if there duplicate values:

for (int = 0; < fourarray.length; i++) {     (int j = + 1; j < fourarray.length; j++)         if (fourarray[i] == fourarray[j]) {             system.out.println("no sudoku");         } else {             system.out.println("sudoku!);         }  } 

however not working. want iterate through arrays find duplicate values, , if there none, print out "sudoku!" if there duplicate values, want print out "sudoku!" need sort array @ all? or there method not aware of? have included program. thank time.

import java.util.scanner;   public class sudoku {      /**      * @param args      */     public static void main(string[] args) {         // todo auto-generated method stub         int boardsize = -1;         int[][] fourarray = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };         int[][] ninearray = { {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0} };          while (true)         {             scanner boardsizeoption = new scanner(system.in);             system.out.println("please select board size:" + "\n" + "1) 4x4" + "\n" + "2) 9x9");             boardsize = boardsizeoption.nextint();             if (boardsize == 1 || boardsize == 2) {                 break;             }         }         if (boardsize == 1) { //still need build complete board              int i, j = 0;             (i = 0; < fourarray.length; i++)             {                 (j = 0; j < fourarray.length; j++)                     system.out.print(fourarray[i][j] + " ");                 system.out.println();             }         } else if (boardsize == 2) {              int i, j = 0;             (i = 0; < ninearray.length; i++)             {                 (j = 0; j < ninearray.length; j++)                     system.out.print(ninearray[i][j] + " ");                 system.out.println();             }     }         int dataselection = -1;              while (true)         {             scanner rowcolumn = new scanner(system.in);             system.out.println("please select way enter values:" + "\n" + "1) row" + "\n" + "2) columnn");             dataselection = rowcolumn.nextint();             if (dataselection == 1 || dataselection == 2) {                 break;             }         }         //entering rows         //this 4x4 board size using rows         if (dataselection == 1) {             if (boardsize == 1) {                 int row = 1;                 while (row < 5) {                     string row1values4x4 = "-1";                     while (true) {                         scanner firstrow4x4 = new scanner(system.in);                         system.out.println("please enter 4 values using commas row " + row); //this needs loop                         row1values4x4 = firstrow4x4.next();                         row1values4x4 = row1values4x4.replaceall(" ",""); //this in case user enters numbers spaces                         if (row1values4x4.length() == 7) {                             break;                         }                     }                     string strarray[] = row1values4x4.split(",");                     int arraysidesinteger[] = new int[strarray.length];                     (int = 0;  < strarray.length;  i++) {                         arraysidesinteger[i] = integer.parseint(strarray[i]);                     }                     fourarray[row-1] = arraysidesinteger;                     (int = 0; < fourarray.length; i++) {                         (int j = 0; j < fourarray.length; j++)                             system.out.print(fourarray[i][j] + " ");                         system.out.println();                     }                     row++;                 }                 //this 9x9 board size using rows                  } else {                      int row = 1;                     while (row < 10) {                         string row1values9x9 = "-1";                         while (true) {                             scanner firstrow9x9 = new scanner(system.in);                             system.out.println("please enter 9 values using commas row " + row); //this needs loop                             row1values9x9 = firstrow9x9.next();                             row1values9x9 = row1values9x9.replaceall(" ",""); //this in case user enters numbers spaces                             if (row1values9x9.length() == 17) {                                 break;                             }                         }                         string strarray[] = row1values9x9.split(",");                         int arraysidesinteger[] = new int[strarray.length];                         (int = 0;  < strarray.length;  i++) {                             arraysidesinteger[i] = integer.parseint(strarray[i]);                         }                         ninearray[row-1] = arraysidesinteger;                         (int = 0; < ninearray.length; i++) {                             (int j = 0; j < ninearray.length; j++)                                 system.out.print(ninearray[i][j] + " ");                             system.out.println();                         }                         row++;                     }                 }             //entering columns             //this 4x4 board size using columns              } else {                  if (boardsize == 1) {                     int column = 1;                     while (column < 5) {                         string column1values4x4 = "-1";                          while (true) {                             scanner firstcolumn4x4 = new scanner(system.in);                             system.out.println("please enter 4 values using commas column " + column);                             column1values4x4 = firstcolumn4x4.next();                             column1values4x4 = column1values4x4.replaceall(" ","");                             if (column1values4x4.length() == 7) {                                 break;                             }                         }                         string strarray[] = column1values4x4.split(",");                         int arraysidesinteger[] = new int[strarray.length];                         (int = 0;  < strarray.length;  i++) {                             arraysidesinteger[i] = integer.parseint(strarray[i]);                         }                         (int = 0; < arraysidesinteger.length; i++) {                             fourarray[i][column-1] = arraysidesinteger[i];                         }                         (int = 0; < fourarray.length; i++) {                             (int j = 0; j < fourarray.length; j++)                                 system.out.print(fourarray[i][j] + " ");                             system.out.println();                         }                         column++;                     }                 //this 9x9 board size using columns                 } else {                      int column = 1;                     while (column < 10) {                         string column1values9x9 = "-1";                         while (true) {                             scanner firstcolumn9x9 = new scanner(system.in);                             system.out.println("please enter 9 values using commas column " + column);                             column1values9x9 = firstcolumn9x9.next();                             column1values9x9 = column1values9x9.replaceall(" ","");                             //row1values4x4 = row1values4x4.replaceall(",","");                             if (column1values9x9.length() == 17) {                                 break;                             }                         }                         string strarray[] = column1values9x9.split(",");                         int arraysidesinteger[] = new int[strarray.length];                         (int = 0;  < strarray.length;  i++) {                             arraysidesinteger[i] = integer.parseint(strarray[i]);                         }                         (int = 0; < arraysidesinteger.length; i++) {                             ninearray[i][column-1] = arraysidesinteger[i];                         }                         (int = 0; < ninearray.length; i++) {                             (int j = 0; j < ninearray.length; j++)                                 system.out.print(ninearray[i][j] + " ");                       system.out.println();                     }                     column++;                 }             }             (int = 0; < fourarray.length; i++) {                 for(int j = + 1; j < fourarray.length; j++) {                     if(fourarray[i] == fourarray[j]) {                         system.out.println("no sudoku");                     } else {                         system.out.println("sudoku!");                     }             }         }     } } 

since it's homework, i'm going minimize code, think you'll fine figure out if have more info 2d arrays, of tricky:

  • since fourarray array of arrays, fourarray[i] refers array (you can think of i-th row of 2-d array).
  • to access individual integers within array of arrays, use fourarray[i][j].
  • if myarray1 == myarray2 (as code @ moment), not compare contents; rather, checks if same array (as happen if said myarray1 = myarray2 first).
  • if did want compare contents of 2 arrays, can use arrays.equals(myarray1, myarray2).
  • as follows above points, fourarray.length size in 1 dimension; fourarray[x].length size in other dimension (where x doesn't matter long it's between 0 , fourarray.length - 1).

added in response comment: understanding , assumption trying avoid duplicate values between of values contained in 2-d fourarray. there number of solutions.

what might called naive solution first use pair of nested for loops go through each value in fourarray. each of values, compare every other value. intermediate code might this:

for (int = 0; < fourarray.length; i++) {     (int j = 0; j < fourarray[i].length; j++) {         // todo: compare value @ (i,j) every other point nesting         // 2 more loops new iterators (called, e.g., m , n)         // todo: if duplicate found, either stop searching, or @         // least mark duplicate has been found somehow.     } } 

on 1 hand, bit inefficient. on other, small 2-d arrays it's still totally trivial computationally, if makes sense you, , move on other problems.

however, i'll submit idea consideration, should interested , assumption allowed values part of sequential set (i.e., in typical sudoku games have 3x3 boxes within allowed values 1-9, never higher). if had array count[] kept track of how many times these known values had occurred? values in initialized zero. iterated through each spot in table (as shown in above code sample), use value found--call v--to increment count[v]. value in count[] greater 1 represents duplicate.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -