patternjavaMinor
Hello Java World ~> Parsing a Sudoku Grid
Viewed 0 times
gridjavaworldparsinghellosudoku
Problem
This is my first, very-very first attempt at java. I haven't started tackling the actual resolution of the sudoku puzzle (not even sure where to start, haven't looked at other weekend-challenge entries yet). I don't have much to go over here, but I'm sure it's filled with beginner mistakes already, so before I make things any worse I'd like to get a review of what I have.
The puzzle being parsed is the one provided as an example in the winning weekend-challenge suggestion.
The program takes the string and outputs this:
Here's the
```
/** A 9x9 Sudoku grid */
public class SudokuGrid {
private SudokuDigit[][] digits;
/** Creates a new Su
The puzzle being parsed is the one provided as an example in the winning weekend-challenge suggestion.
public class SudokuGame {
private final static String newLine = System.lineSeparator();
private final static String puzzle =
"...84...9" + newLine +
"..1.....5" + newLine +
"8...2146." + newLine +
"7.8....9." + newLine +
"........." + newLine +
".5....3.1" + newLine +
".2491...7" + newLine +
"9.....5.." + newLine +
"3...84...";
public static void main(String[] args){
SudokuGrid grid = new SudokuGrid(puzzle);
System.out.print(grid);
}
}The program takes the string and outputs this:
+===========+===========+===========+
| | | | 8 | 4 | | | | 9 |
-------------------------------------
| | | 1 | | | | | | 5 |
-------------------------------------
| 8 | | | | 2 | 1 | 4 | 6 | |
+===========+===========+===========+
| 7 | | 8 | | | | | 9 | |
-------------------------------------
| | | | | | | | | |
-------------------------------------
| | 5 | | | | | 3 | | 1 |
+===========+===========+===========+
| | 2 | 4 | 9 | 1 | | | | 7 |
-------------------------------------
| 9 | | | | | | 5 | | |
-------------------------------------
| 3 | | | | 8 | 4 | | | |
+===========+===========+===========+Here's the
SudokuGrid class:```
/** A 9x9 Sudoku grid */
public class SudokuGrid {
private SudokuDigit[][] digits;
/** Creates a new Su
Solution
-
Instead of your
-
This can be marked with final:
-
-
-
A double for-loop with variables
-
-
Simplification possible in your
-
When writing JavaDoc, I believe it's convention to use it on multiple lines. Or at least that's more common than using only one line (Also, did you know that you could write
-
You can use
-
You can use
If I wouldn't have known it, I wouldn't be able to tell that you are a Java beginner. You seem to be following nearly all of the common conventions and seem to be doing things well overall. Not much to complain about, really.
Instead of your
String puzzle you could use an String[] like below (not saying you have to, but you should know that the alternative exists). You are currently converting it to a String array later anyway, so why not have it as a String array right from the start?private final static String[] puzzle = new String[]{ "...84...9", "..1.....5", ... };-
This can be marked with final:
private SudokuDigit[][] digits; to prevent any malicious programmer from even trying to use this.digits = null after it has once been initialized, or initialize a new 2-dimensional array-
3 is a magic number. Yes it is, it's a magic number. In the past in the present and the future, I say three, is a magic number.-
9 is also magic number. This means that you should use it as a constant:public static final int MAGIC_NUMBER = 9;-
A double for-loop with variables
i and j can be called x and y instead, to avoid any possible confusion about which one is which.-
UnsupportedOperationException might not be the best exception to throw in your setDigit method. IllegalArgumentException would be better, since the argument to the method is incorrect.-
Simplification possible in your
SudokuDigit char constructor:numericValue = value == '.' ? null : Integer.parseInt(String.valueOf(value));-
When writing JavaDoc, I believe it's convention to use it on multiple lines. Or at least that's more common than using only one line (Also, did you know that you could write
/** before a method in Eclipse and then press enter to make Eclipse write the basic "layout" of it for you?)/**
* Creates a new digit from specified character.
*/
public SudokuDigit(char value) {-
You can use
@throws in Javadoc to specify What exception can be thrown, and why./**
* Sets the current value of the digit.
* Specify null for no value.
* @throws UnsupportedOperationException if value is not valid.
*/
public void setDigit(Integer value) {-
You can use
Character.digit(char c, int radix) instead of Integer.parseInt to convert your chars to int. Such as Character.digit(value, 10)If I wouldn't have known it, I wouldn't be able to tell that you are a Java beginner. You seem to be following nearly all of the common conventions and seem to be doing things well overall. Not much to complain about, really.
Code Snippets
private final static String[] puzzle = new String[]{ "...84...9", "..1.....5", ... };public static final int MAGIC_NUMBER = 9;numericValue = value == '.' ? null : Integer.parseInt(String.valueOf(value));/**
* Creates a new digit from specified character.
*/
public SudokuDigit(char value) {/**
* Sets the current value of the digit.
* Specify null for no value.
* @throws UnsupportedOperationException if value is not valid.
*/
public void setDigit(Integer value) {Context
StackExchange Code Review Q#37435, answer score: 4
Revisions (0)
No revisions yet.