3

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");
    }
}

2 Answers 2

1

When you are adding the theItem to currentList using currentList.add(theItem), you are adding the all the information of the item (title, date, time, ...).

When you are searching for the task using contains(date), you are only searching for the date. So what happens behind the scene is that a date(eg, '20/10/2020') is being compared to something much longer (eg, 'myTitle, 20/10/2020, 10pm...') and they do not match, hence no task is shown.

Sign up to request clarification or add additional context in comments.

5 Comments

Is there any way I can search for tasks with "20/10/2020" in them?
Personally, I would have currentList in the type of Task (so List<Task> currentList). So when you add a task to currentList, you would add an instance of task. This would make the search easier because you now have access to the getDate() method. Now you can loop through the currentList and compare the date directly using currentList[i].getDate().equals(data). Let me know if that makes sense.
Would you be able to go a bit more in depth with this? I'm quite new to Java sorry. When I change String to Task in the ArrayList, I get multiple errors throughout my code because the arrayList is no longer a type String
@DrMe you would need to make additional changes apart from simply changing the ArrayList type. For instance, you would need to change the showList() method, where your enhanced for loop would iterate through Task objects, instead of String objects. Similarly, you would also need to change how you're adding items, in the addItem() method.
I have made these changes. However, when I change the line String theItem = myTaskObj.getItem(); to Task theItem = myTaskObj.getItem();, I receive an error saying: Type mismatch: cannot convert from String to Task. I have changed the return type of getItem(), however, this then just creates more errors.
1

In addition to keyboardwarrior's answer, yes one way to do it would have been to have List<Tast> currentList as your ArrayList.

However, if you're unwilling to change your code, you can workaround it by doing so:

1)Loop through the items(of type String) in your currentList
2)Split the item using the ", " as a separator, ie item.split(", ")
3)During your conversion to String in getItem(), the date is the second thing you're adding to the string, ie the first thing after the first comma(,).
So item.split(", ")[1] is what's gonna contain the date.
4)Compare this to the date that you want to search, in every loop. Break when you find it.

boolean found = false;
for (String task: currentList){
       String searched_date = task.split(", ")[1];
       if searched_date.equals(date){
           found = true;
           System.out.println(task);
       }
}
if (!found){  // if there was no task found for the specified date
    System.out.println("No Task Found");
}

8 Comments

Could you show how you would do this with code please? I am quite new to java.
@DrMe I've edited my answer to include that in code.
I've updated my code now in the question, how do I actually display the list of tasks on that specific date now?
@DrMe You could perhaps just add an SOP statement there, I've editted my code again to do that. Note: Originally I thought, you were interested in only the first matching task, so I was breaking out of the loop. Since you want all matching tasks, I've removed the break statement.
I have updated my code, this time using Date instead of String for the date of the Task. However, when I do this, I receive an error saying: Unlikely argument type for equals(): Date seems to be unrelated to String.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.