bean
bean

Reputation: 337

How to search for elements in an ArrayList? - Java

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

Upvotes: 3

Views: 385

Answers (2)

Ayush
Ayush

Reputation: 1620

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

Upvotes: 1

keyboardwarrior
keyboardwarrior

Reputation: 11

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.

Upvotes: 1

Related Questions