From 2209dd7786a499809b69625477b3fbf84b11d88a Mon Sep 17 00:00:00 2001 From: Imbus Date: Mon, 11 Dec 2023 13:50:58 +0100 Subject: [PATCH] Ugly fix for invalid input --- app/src/main/java/gui/SudokuController.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/gui/SudokuController.java b/app/src/main/java/gui/SudokuController.java index 69def49..98c2f31 100644 --- a/app/src/main/java/gui/SudokuController.java +++ b/app/src/main/java/gui/SudokuController.java @@ -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);