Note that there are some explanatory texts on larger screens.

plurals
  1. POIn search of some guidance regarding HashMap
    primarykey
    data
    text
    <p>I'm making a Sudoku Solver program in Java, which uses backtracking and bruteforce for the solving algorithm. The problem I seem to be having is that when I insert the correct values in a simple container, the wrong values are showing when i try to access it from another class. </p> <p>HashMap has the correct values in the correct locations when I print it out from inside the insert method in <code>Sudokucontainer</code>, (with a <code>for(int i = 0 i &lt; dim; i++) + for(int j = 0; j &lt; dim; j++)</code>) followed by <code>Square[][] tmp = solutionsHash.get(count)</code> and <code>System.out.println(tmp[i][j].getValue());</code>. It's also successful when I use the same for-loops right after the <code>solutions.insert</code> statement in the <code>Square</code> class' <code>fillInnRemainingOfBoard()</code> (followed by <code>Square[][] tmp = solutions.solutionsHash.get(count)</code> and the same <code>System.out.println</code> statement.</p> <p>The problem occurs when I try to do the same from the <code>Board</code> class, as you can see in my test() method in that class. Then it just prints out the predefined values(or nothing if the board is empty). The only difference is that I then use i.e. <code>Square[][] tmp = allSquares[0][0].solutionsHash.get(wantedNumber)</code> and the same as before. But as I mentioned, I don't see why that should make a difference, when <code>solutionsHash</code> are declared static in the <code>Sudokucontainer</code>-class. </p> <p>Any help is greatly appriciated. Thanks!</p> <p><strong>NOTE:</strong> The indentation got screwed up somehow, sorry about that. I haven't included the GUI part because I haven't written that and I possibly don't have permission to put it on here. Also the problem occurs before that(possibly in the <code>Board</code> class), as the same values are printed out there as from the <code>Board</code> class.</p> <p><strong>EDIT:</strong> Removed some unnecessary code.</p> <pre><code>public class Square { static Sudokucontainer solutions; protected char value; protected int valueInt; protected static Square allSquares[][]; public int count = 0; protected boolean predefined = false; protected boolean lastSquare = false; protected Square next = null; public void setNext() { if(col.getNr() &lt; col.dim) { next = allSquares[row.getNr()-1][col.getNr()]; }else if(col.getNr() == col.dim) { if(row.getNr() &lt; row.dim) { next = allSquares[row.getNr()][0]; }else { next = null; lastSquare = true; } } if(next != null) { if(next.col.getNr() == col.dim &amp;&amp; next.row.getNr() == col.dim &amp;&amp; next.predefined == true) lastSquare = true; } } public void fillInnRemainingOfBoard(Square[][] allSquares, int hd, int br) { this.allSquares = allSquares; int highestNumber = col.dim; setNext(); if(value == '\u0020') { for(int i = 1; i &lt;= highestNumber; i++) { if(row.isLegal[i-1] &amp;&amp; col.isLegal[i-1] &amp;&amp; box.isLegal[i-1]) { String s1 = Integer.toString(i); value = s1.charAt(0); insert(value); insertInt(i); box.isLegal[i-1] = false; col.isLegal[i-1] = false; row.isLegal[i-1] = false; if(!lastSquare) { next.fillInnRemainingOfBoard(this.allSquares, hd, br); }else{ System.out.println("Solution found: " + count); solutions.insert(allSquares, count, col.dim); ++count; } // Slutt paa else box.isLegal[valueInt-1] = true; col.isLegal[valueInt-1] = true; row.isLegal[valueInt-1] = true; value = '\u0020'; } // Slutt paa if(row.isLegal[i-1] &amp;&amp; col.isLegal[i-1] &amp;&amp; box.isLegal[i-1]) } // Slutt paa if (value == '\u0020' || !predefined) }else { if(!lastSquare) { next.fillInnRemainingOfBoard(this.allSquares, hd, br); } else { System.out.println("Solution found"); solutions.insert(allSquares, count, col.dim); count++; } } // Slutt paa else if(predefined) // Slutt paa for(int i = 1; i &lt;= highestNumber; i++) }// Her returnerer vi til forrige metode public void insert(char value) { if(value != '.') { this.value = value; }else{ this.value = '\u0020'; // In other words; space(in unicode) } } public void insertInt(int value) { valueInt = value; } public char getValue() { return value; } } public class Sudokucontainer { int dim = 0; private int count = 0; static HashMap&lt;Integer, Square[][]&gt; solutionsHash = new HashMap&lt;Integer, Square[][]&gt;(); public void insert(Square[][] allSquares, int count, int dim) { this.count = count; this.dim = dim; if(count &lt;= 499) { solutionsHash.put(count,allSquares); } } public Square[][] get(int nr) { Square[][] tmp = solutionsHash.get(nr); return tmp; } public int getSolutionCount() { return solutionsHash.size(); } } public class Board { int dim, br, hd; char[][] charArray; char[] charArray1; static Square [][] allSquares; Row [] rows; Column[] columns; Box[] boxes; public Board(int dim, int br, int hd, char[][] charArray) { this.dim = dim; this.br = br; this.hd = hd; this.charArray = charArray; charArray1 = new char[dim]; allSquares = new Square[dim][dim]; rows = new Row[dim]; columns = new Column[dim]; boxes = new Box[dim/br * dim/hd]; setRows(); setColumns(); setBoxes(); setSquares(); insertBoxInSquares(); fillInLegalValues(); welcome(); solve(); test(); System.out.println("Found solutions. Please refer to the graphical interface."); showGui(); } public void test() { for(int i = 0; i &lt; dim; i++) { for(int j = 0; j &lt; dim; j++) { //Square[][] tmp = allSquares[0][0].solutions.solutionsHash.get(0); //System.out.println(tmp[i][j].getValue()); } } } public void solve() { allSquares[0][0].fillInnRemainingOfBoard(allSquares, hd, br); } public void showGui() { new SudokuGUI(dim, hd, br, allSquares[0][0].solutions.solutions, false, 0, allSquares[0][0].solutions.solutionsHash); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload