I have created a To-Do-List program in java where you are able to add, remove, and view tasks with specific names, dates, times etc.
However, I am trying to add an option where when the user selects "5", they are able to input a date, and if any tasks fall on that particular date, they will be listed.
For example:
----------------------
Main Menu
----------------------
1. Add a task
2. Delete a task
3. Delete all tasks
4. List all tasks
5. Search for a task
6. Exit the program
Enter choice: 5
Enter a date: 20/10/2020
Here are the tasks for 20/10/2020:
-task1-
-task2-
etc...
So far when I do this, no tasks are returning even if they do exist on that specific date.
Here is my code so far:
public void searchTasks() throws ParseException {
System.out.println("Search for a task: ");
System.out.println("----------------------");
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a date (dd/mm/yyyy): ");
Scanner scanner = new Scanner(System.in);
String date = scanner.nextLine();
LocalDate theDate = LocalDate.parse(date, formatter);
String backToStr = formatter.format(theDate);
boolean found = false;
for (String task : currentList) {
String searched_date = task.split(", ")[1];
if (searched_date.equals(backToStr)) {
found = true;
System.out.println();
System.out.println("----------------------");
System.out.println("Here are the tasks on " + backToStr + ":");
System.out.println("----------------------");
System.out.println(task);
}
}
// if there was no task found for the specified date
if (!found) {
System.out.println("No tasks found on this date");
}
}