From 6facc040a6428dcfac1367098eeceb13a6f7ba97 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 3 Feb 2021 13:54:28 +0100 Subject: [PATCH] added input validation --- CorrectionHelper/src/CorrectionHelper.java | 61 +++++++++++++--------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/CorrectionHelper/src/CorrectionHelper.java b/CorrectionHelper/src/CorrectionHelper.java index 599b203..93c4762 100644 --- a/CorrectionHelper/src/CorrectionHelper.java +++ b/CorrectionHelper/src/CorrectionHelper.java @@ -12,12 +12,13 @@ import java.util.Scanner; public class CorrectionHelper { final static String FILE_PATH = "./answers.xls"; - final static boolean CORRECT_ALL = false; + final static boolean CORRECT_ALL = true; final static String TASK_TO_CORRECT = "1.2"; final static int LAST_NAME = 0; final static int FIRST_NAME = 1; final static int MATRICULATION_NUMBER = 2; + public static void main(String[] args) { File file = new File(FILE_PATH); @@ -53,16 +54,21 @@ public class CorrectionHelper { for (int r = 0; r < rows; r++) { row = sheet.getRow(r); + if (row != null) { boolean inRow = true; + for (int c = 0; c < cols; c++) { cell = row.getCell((short) c); + if (cell != null) { try { if (cell.getStringCellValue().contains("MANUAL_CORRECTION")) { + if (inRow) System.out.println(row.getCell(FIRST_NAME) + " " + row.getCell(LAST_NAME) + " " + row.getCell(MATRICULATION_NUMBER)); + inRow = false; int column = row.getCell(c - 1).getColumnIndex(); @@ -70,33 +76,38 @@ public class CorrectionHelper { if (task.contains(TASK_TO_CORRECT) || CORRECT_ALL) { - Scanner scanner = new Scanner(System.in); - System.out.print(task + ": "); - String input = scanner.nextLine(); + boolean inputOk = false; - if (input.equals("SAVE")) { - wb.write(file); - System.exit(0); + while (!inputOk) { + Scanner scanner = new Scanner(System.in); + System.out.print(task + ": "); + String input = scanner.nextLine(); - // FIXME: only skip one entry - } else if (input.equals("SKIP")) { - break; + if (input.equals("SAVE")) { + wb.write(file); + System.exit(0); + + } else if (input.equals("SKIP")) { + break; + } else { + int rating = Integer.parseInt(input); + if (0 <= rating && rating <= 100) { + inputOk = true; + row.getCell(column).setCellValue(Integer.parseInt(input)); + wb.write(file); + + if (Integer.parseInt(input) != 100) { + System.out.print("Comment: "); + String comment = scanner.nextLine(); + + row.getCell(column + 3).setCellValue(comment); + wb.write(file); + } + row.getCell(c).setCellValue("CORRECTED_MANUAL"); + wb.write(file); + } + } } - - // TODO: validate input - - row.getCell(column).setCellValue(Integer.parseInt(input)); - wb.write(file); - - if (Integer.parseInt(input) != 100) { - System.out.print("Comment: "); - String comment = scanner.nextLine(); - - row.getCell(column + 3).setCellValue(comment); - wb.write(file); - } - row.getCell(c).setCellValue("CORRECTED_MANUAL"); - wb.write(file); } } } catch (Exception ignored) {