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

8
  • 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. Commented Nov 10, 2021 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. Commented Nov 10, 2021 at 16:21
  • Could you provide a code example ? Commented Nov 10, 2021 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). Commented Nov 10, 2021 at 17:56
  • 2
    @mrwahl I suggest you start by reading about classes and how to create instances. Commented Nov 10, 2021 at 17:57

1 Answer 1

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 ;
    }
}
Sign up to request clarification or add additional context in comments.

12 Comments

Don't use Date and SimpleDateFormat.
And you should properly model a task, instead of using an array. Use objects.
I have taken your comments into account.
It’s not really good. In Task the date should be a LocalDate, not a String. Upper case YYYY in the format pattern string is wrong, you need lower case uuuu or yyyy.
Code fixed, so I removed my down-vote.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.