Skip to content

Commit

Permalink
Check for invalid task index, this fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
waffledood committed Jan 14, 2022
1 parent cc8ff0c commit 006262d
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ else if (userInput.toLowerCase().equals("list")) {

// Mark, Unmark - "mark itemIndexNumber", "unmark itemIndexNumber", marks an item as done/undone accordingly
else if (userInput.toLowerCase().matches("^mark \\d+") || userInput.toLowerCase().matches("^unmark \\d+")) {
String[] stringArray = userInput.toLowerCase().split(" ");
int taskIndex = Integer.valueOf(stringArray[1]) - 1;

try {
String[] stringArray = userInput.toLowerCase().split(" ");
int taskIndex = Integer.valueOf(stringArray[1]) - 1;

// if user-specified task index is out of the list
if (taskIndex >= taskList.size() || taskIndex < 0) {
throw new DukeException("I'm sorry, you're referencing a task that does not exist!");
}

// Mark
if (stringArray[0].equals("mark")) {

Expand All @@ -77,8 +83,8 @@ else if (userInput.toLowerCase().matches("^mark \\d+") || userInput.toLowerCase(
System.out.println(String.format("%sOkay, marking this task as not done yet:", indentationText));
System.out.println(String.format("%s%s", indentationTaskStatus, task));
}
} catch (IndexOutOfBoundsException e) {
System.out.println(String.format("%sNo such task exists!", indentationText));
} catch (DukeException e) {
System.out.println(String.format("%s%s", indentationText, e.getMessage()));
}

}
Expand Down Expand Up @@ -130,9 +136,9 @@ else if (userInput.toLowerCase().matches("^todo .*") || userInput.toLowerCase().
}

}
// TODO - catching all Exceptions
catch(Exception e) {
System.out.println("\tException: " + e.getMessage() + ", of Exception type: " + e.getClass().getCanonicalName());

catch(DukeException e) {
System.out.println(String.format("%sException: %s", indentationText, e.getMessage()));
}
}

Expand Down

0 comments on commit 006262d

Please sign in to comment.