import java.util.*; public class SudokuPuzzle { private int[][] board =new int[9
ID: 3614879 • Letter: I
Question
import java.util.*;
public class SudokuPuzzle
{
private int[][] board =new int[9][9];
publicSudokuPuzzle()
{
int[][] board;
board = new int[9][9];
board[0][1] = 2;
}
public void displayBoard(){
System.out.print(toString());
}
public boolean getAllowed() {
return true;
}
// String method of game board used to display board throughoutgame play.
public String toString() {
return " --- --- --- ------ --- --- --- ---" + " | " + board[0][0] + " | " + board[0][0] +" | " + board[0][0] +
" | " + board[0][3] + "| " + board[0][4] + " | " + board[0][5] + " | " + board[0][6] + " |" + board[0][7] +
" | " + board[0][8] + "| " + board[0][9] + " |";
}
}
Explanation / Answer
If the array has 9 elementsthen the index varies from only 0 to 8...therefore you canonly use board[0][0] to board[0][8] you cannot use board[0][9] which will createindex out of bounds exception The correct code to toString() is: // String method of game board used to display boardthroughout game play. public String toString() { return " --- --- --- ------ --- --- --- ---" + " | " + board[0][0] + " | " + board[0][0] +" | " + board[0][0] + " | " + board[0][3] + "| " + board[0][4] + " | " + board[0][5] + " | " + board[0][6] + " |" + board[0][7] + " | " + board[0][8] + "| "; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.