From d056732addea2ffba64a36b6d1a61aed927fb1b9 Mon Sep 17 00:00:00 2001 From: Imbus Date: Mon, 11 Dec 2023 14:25:04 +0100 Subject: [PATCH] Polishing invalid input handling --- app/src/main/java/gui/SudokuController.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/gui/SudokuController.java b/app/src/main/java/gui/SudokuController.java index 5528349..a95f1a4 100644 --- a/app/src/main/java/gui/SudokuController.java +++ b/app/src/main/java/gui/SudokuController.java @@ -94,24 +94,31 @@ public class SudokuController { int row = view.getSelectedRow(); int col = view.getSelectedColumn(); + // The value to be inserted into the cell + // Zero inicates an empty cell + // Negative values are invalid int value = 0; String cellValue = view.getCellValue(row, col); + + // We need to check for null and empty string if (cellValue == null || cellValue.equals("")) { value = 0; } else { try { value = Integer.parseInt(cellValue); } catch (NumberFormatException ex) { - value = 0; + value = -1; } } - // If the input is invalid - if (!model.isLegal(row, col, value)) { + // If the input is invalid, value < 0 indicates parse error + if (!model.isLegal(row, col, value) || value < 0) { value = 0; view.showErrorMessage("Invalid input. Try again."); } + + // Update the model and view model.set(row, col, value); view.updateView(model.getBoard()); }