简体   繁体   中英

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 (!found){  // if there was no task found for the specified date
    System.out.println("No tasks found on this date");
}

}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM