Skip to content

Commit

Permalink
Address bug of scanner not closing after exitCommand.
Browse files Browse the repository at this point in the history
Running with Duke with jave -jar command shows that scanner remains open and program does not terminate properly after exitCommand given.

Let's add a boolean to check for exitCommand and close scanner appropriately.

exitCommand now terminates program successfully
  • Loading branch information
samlsm committed Aug 23, 2020
1 parent e2bd514 commit 643299a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duke;

import duke.command.Command;
import duke.command.ExitCommand;

import java.util.Scanner;

Expand All @@ -25,15 +26,19 @@ public static Duke createDuke(String filePath) {

public void run() {
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
boolean isExit = false;
while (!isExit && input.hasNextLine()) {
try {
String commandMessage = input.nextLine();
Command c = Parser.parse(commandMessage);
c.execute(commandMessage, storage, ui);
if (c instanceof ExitCommand) {
isExit = true;
input.close();
}
} catch (DukeException ex) {
System.out.println(ex.getMessage());
}
}
input.close();
}
}

0 comments on commit 643299a

Please sign in to comment.