Skip to content

Commit

Permalink
Trim whitespaces and improve storage reading
Browse files Browse the repository at this point in the history
  • Loading branch information
aureliony committed Feb 18, 2024
1 parent c9081b8 commit e8a4dd0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ application {
shadowJar {
archiveBaseName = "convobot"
archiveClassifier = null
dependsOn("distZip", "distTar")
dependsOn("distZip", "distTar");
}

// Configure run task settings
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cvb/commands/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private static void enforceArgumentCount(int a, int b) throws ConvoBotException
* @throws ConvoBotException if the input is invalid or cannot be parsed
*/
public static Command parse(String userInput) throws ConvoBotException {
userInput = userInput.trim(); // remove leading and trailing spaces
ArrayList<String> inputList = new ArrayList<>(Arrays.asList(userInput.split(" ")));
if (inputList.isEmpty()) {
throw new ConvoBotException("Invalid input. Input must not be empty.");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cvb/tasks/TaskParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static Task parse(String line) throws IllegalArgumentException {
final IllegalArgumentException invalidLineException =
new IllegalArgumentException("Invalid line format: " + line);

line = line.trim(); // remove leading and trailing spaces
String[] parts = line.split(" \\| ");
if (parts.length < minPartsLength || parts.length > maxPartsLength) {
throw invalidLineException;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/cvb/utils/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ public ArrayList<Task> read() {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
Task task = TaskParser.parse(line);
Task task;
try {
task = TaskParser.parse(line);
} catch (IllegalArgumentException e) {
continue; // skip this line
}
taskList.add(task);
}
reader.close();
} catch (IOException e) {
// ignored as taskList will be returned empty
return taskList;
}
return taskList;
}
Expand Down

0 comments on commit e8a4dd0

Please sign in to comment.