Ugly fix for invalid input

This commit is contained in:
Imbus 2023-12-11 13:50:58 +01:00
parent 3cf7005151
commit 2209dd7786

View file

@ -94,7 +94,22 @@ public class SudokuController {
int row = view.getSelectedRow();
int col = view.getSelectedColumn();
int value = Integer.parseInt(view.getCellValue(row, col));
int value = 0;
String cellValue = view.getCellValue(row, col);
if (cellValue == null || cellValue.equals("")) {
value = 0;
} else {
try {
value = Integer.parseInt(cellValue);
} catch (NumberFormatException ex) {
model.set(row, col, 0);
view.updateView(model.getBoard());
view.showErrorMessage("Invalid input. Try again.");
return; // Bail out if the input is invalid
}
}
// Check if the input is legal and update the model and view
if (model.isLegal(row, col, value)) {
model.set(row, col, value);