How to search for elements in an ArrayList? - Java

by DrMe   Last Updated June 30, 2020 04:26 AM

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: ( searchTasks() is at the bottom of the code )

import java.util.ArrayList;
import java.util.List;
import java.text.ParseException;
import java.util.Scanner; 

class Task{

private String theTitle;
private String theDate;
private String theTime;
private String theLocation;
private String theDuration;
private String theCategory;

Task(String title, String date, String time, String location, String duration, String category) {
    
    theTitle = title;
    theDate = date;
    theTime = time;
    theLocation = location;
    theDuration = duration;
    theCategory = category;
    
}

public String getTitle() {
    
    return theTitle;
}

public String getDate() {
    
    return theDate;
}

public String getTime() {
    
    return theTime;
}

public String getLocation() {
    
    return theLocation;
}

public String getDuration() {
    
    return theDuration;
}

public String getCategory() {
    
    return theCategory;
}

public String getItem() {
    
    return theTitle + ", " + theDate + ", " + theTime + ", " + theLocation + ", " + theDuration + ", " + theCategory;
}

}


public class ToDoList {

public Task myTaskObj;
private static List<String> currentList = new ArrayList<String>();

public ToDoList() {
    
}

public static void main (String[] args) {
    
    ToDoList listObj = new ToDoList();
    
    int menuItem = -1;
    while (menuItem != 6) {
        menuItem = listObj.printMenu();
        switch (menuItem) {
        case 1:
            listObj.addItem();
            break;
        case 2:
            listObj.removeItem();
            break;
        case 3:
            listObj.removeAllTasks();
            break;
        case 4:
            listObj.showList();
            break;
        case 5:
            listObj.searchTasks();
            break;
        case 6: 
            System.out.println("Goodbye!");
        default:
            System.out.println("Enter a valid option");
        }
    }   
    
}

public int printMenu() {
    
    Scanner scanner = new Scanner(System.in);
    System.out.println();
    System.out.println("----------------------");
    System.out.println("Main Menu");
    System.out.println("----------------------");
    System.out.println("1. Add a task");
    System.out.println("2. Delete a task");
    System.out.println("3. Delete all tasks");
    System.out.println("4. List all tasks");
    System.out.println("5. Search for a task");
    System.out.println("6. Exit the program");
    System.out.println();
    System.out.print("Enter choice: ");
    int choice = scanner.nextInt();
    
    return choice;
    
}

public void showList() {
System.out.println();
System.out.println("----------------------");       
System.out.println("To-Do List");
System.out.println("----------------------");
int number = 0;
for (String item : currentList) {
    System.out.println(++number + ". " + item);
}
System.out.println("----------------------");


}

public void addItem() {
System.out.println("Add a task");
System.out.println("----------------------");

System.out.print("Enter the task title: ");
Scanner scanner = new Scanner(System.in);
String title = scanner.nextLine();


System.out.print("Enter the task date: ");
Scanner scanner2 = new Scanner(System.in);
String date = scanner2.next();


System.out.print("Enter the task time: ");
Scanner scanner3 = new Scanner(System.in);
String time = scanner3.nextLine();

System.out.print("Enter the task location: ");
Scanner scanner4 = new Scanner(System.in);
String location = scanner4.nextLine();

System.out.println("Enter the task duration (optional - press enter to skip): ");
Scanner scanner5 = new Scanner(System.in);
String duration = scanner5.nextLine();

System.out.println("Enter the task category (optional - press enter to skip): ");
Scanner scanner6 = new Scanner(System.in);
String category = scanner6.nextLine();

myTaskObj = new Task(title, date, time, location, duration, category);

String theItem = myTaskObj.getItem();

currentList.add(theItem);
System.out.println("Task Added!");



}

public void removeItem() {
System.out.println("Delete a task");
System.out.println("----------------------");
Scanner scanner = new Scanner(System.in);
System.out.print("What do you want to remove? (Enter number): ");
int index = scanner.nextInt();
if((index-1)<0 || index>currentList.size()) {
    System.out.println("Wrong index number! Please enter in range of 1 to "+currentList.size());            
}else {
    currentList.remove(index-1);
}
System.out.println("----------------------");
System.out.println("Task Removed!");


}

public void removeAllTasks() {

System.out.println("Remove all tasks");
System.out.println("----------------------");
showList();

Scanner keyboard = new Scanner(System.in);
System.out.print("Are you sure you'd like to delete all tasks? 'Yes' or 'No': ");
String choice = keyboard.nextLine();
if(choice.equals("Yes")) {
    currentList.removeAll(currentList);
    System.out.println("All tasks deleted!");
}
else 
    if(choice.equals("No"))
    System.out.println("Tasks not deleted");

}

public void searchTasks() {
System.out.println("Search for a task: ");
System.out.println("----------------------");
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a date: ");
String date = keyboard.nextLine();

boolean ans = currentList.contains(date);

if (ans)
    System.out.println("Here are the tasks for " + date);

else
    System.out.println("No tasks on this date.");
    }
}
Tags : java arraylist


Answers 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.

keyboardwarrior
keyboardwarrior
June 30, 2020 04:25 AM

Related Questions






Sort object List by another List using Java Comparators

Updated February 07, 2019 04:26 AM