-3

I've searched multiple posts and can't find something specific for my needs.

** The goal**

I need to take a user input in the form of Date and then a String description and store it in an Arraylist. i.e 10 Nov 2021, Clean my room
I want my date to be in D MMM YYYY format

Once this is stored in the arraylist, I need to be able to search the arraylist of tasks for the current date
If my arraylist contains a task for 11 Nov 2021 and current date/local machine date is 11 Nov 2021, I need to print the task for today , else print out that no tasks exist for the current date.

Here is a snippet of what I've tried so far

public class TestClass {
    
    public static void main(String[]args){
 
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         
      try{
          System.out.println("Enter : ");  
   // sample string   
        String str = br.readLine();              
        List<String> list = Arrays.asList(str.split(","));
        System.out.println("--------------");
        System.out.println(list);
      }
    catch(IOException e){
        System.out.println(e);
    }
      
}
  
}

Here is an output

Enter : 
10 jan 2020, clean room
--------------
[10 jan 2020,  clean room]

I'm not sure how to proceed next, thanks in advance.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
mrwahl
  • 79
  • 1
  • 9
  • 6
    I suggest creating a class to store each date and string, maybe named `Task` or something else appropriate. Then you can create an array list of instances of the class. – Code-Apprentice Nov 10 '21 at 16:18
  • 1
    Then the next step is to write a method where you can search the list of tasks for one on a certain day. – Code-Apprentice Nov 10 '21 at 16:21
  • Could you provide a code example ? – mrwahl Nov 10 '21 at 17:03
  • 1
    I’m afraid that we are awaiting a bit more effort on your part. The web is filled with code examples. You will find many examples of a model class/POJO class that you can use as inspiration for your `Task` class. You will find many examples of how to split a string at the first comma (or other predefined character). Many examples of how to parse a date string into a `LocalDate` (only avoid example using the troublesome and long outdated `SimpleDateFormat` and `Date` classes). – Ole V.V. Nov 10 '21 at 17:56
  • If any of that turns out not to be as trivial as it sounds (which is likely to be the case), please show us your attempt in a new question, and we will be here to help, promise. Feel free to drop a link to your new question in a comment here. – Ole V.V. Nov 10 '21 at 17:56
  • 2
    @mrwahl I suggest you start by reading about classes and how to create instances. – Code-Apprentice Nov 10 '21 at 17:57
  • Always search Stack Overflow thoroughly before posting. – Basil Bourque Nov 10 '21 at 22:21
  • @BasilBourque Agree, obviously. The OP already reported: *I've searched multiple posts and can't find something specific for my needs.* So if we can give them something more specific about what to search for or how to search, I think it would be helpful. – Ole V.V. Nov 11 '21 at 05:48

1 Answers1

1

I hope this will help you:

    public class Main {

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d MMM yyyy");  
        List<Task> tasks=new ArrayList<Task>();       
        while (true) {
           try{
               System.out.println("Enter : ");  
               String str = br.readLine();
               Task task= new Task(str.split(",")[0],str.split(",")[1]);
               tasks.add(task);
               System.out.println("--------------");
               System.out.println(task.toString());
               System.out.println("--------------");
               System.out.println("Tasks for today");
               LocalDateTime now = LocalDateTime.now();  
               String currentDateStr=dtf.format(now);
               System.out.println(getTasksByDate(tasks,currentDateStr));
            }
            catch(IOException e){
                System.out.println("Enter a valid input");
            }   
        }
         
 }
 public static List<Task> getTasksByDate(List<Task> tasks,String date ){
     List<Task> res=new ArrayList<Task>(); 
     for(int i=0;i<tasks.size();i++) {
         if(tasks.get(i).getDate().equals(date)) {
             res.add(tasks.get(i));
         }
     }
     return res;
 }
}
//Task.java
public class Task {
    private String date;
    private String subject;
    public Task(String date,String subject) {
        this.date=date;
        this.subject=subject;
    }
    public String getSubject() {
        return this.subject;
    }
    public String getDate() {
        return this.date;
    }
    public String toString() {
        return this.date + ":" +this.subject ;
    }
}