如何在Java中搜索文件中特定日期和时间(由用户输入)的方法

huangapple go评论79阅读模式
英文:

How to search a file for a certain date and time (inputted from user) in java

问题

System.out.print("Enter the date of the task: ");
task_date = scan.nextLine();

System.out.print("Enter the time of the task: ");
String task_time = scan.nextLine();

System.out.print("Enter the name of the task: ");
String task_name = scan.nextLine();
String task = task_date + " " + task_time + " " + task_name;
System.out.println(task);

String search_date = task_date;
String search_time = task_time;
int lineNumber = 0;
File taskstxt = new File("tasks.txt");
Scanner filereader = new Scanner(taskstxt);

while (filereader.hasNextLine()) {
    final String lineFromFile = filereader.nextLine();
    if (lineFromFile.contains(search_date)) {
        // Handle existing task
    }
}

// Append the file with the new task date/time/name

在上面的代码片段中,我想要在用户输入的日期和时间在文件中查找,如果已存在,则打印 "你已经有一个任务安排在那个时间了",如果不存在,则将新任务的日期/时间/名称追加到文件中。

英文:

I am trying to figure out how I would go about searching a .txt file for a certain date and time that is given by the user. Currently my code does this:

PRINT "Enter the date of the task: "
READ into task_date (string)
PRINT "Enter the time of the task: "
READ into task_time (string)
PRINT "Enter the name of the task: "
READ into task_name (string)
LOAD tasks.txt file

I want to be able to read the data from the user in those three steps (yes i know it would probably be better to do it in one step but this is more of a proof of concept to a friend), then search a the tasks.txt file for a space to insert the task_date + task_time + task_name. Any help would be appreciated. Thanks

Edit:
Ok, I am creating a calendar app that is currently terminal based.
What it does is it takes the user inputs task date/time/name and appends it in the correct position in a file called tasks.txt (the file example is below)

10-6-2020 6:30 am Wake up
10-6-2020 7:00 am Take mom to school
10-6-2020 7:30 am arrive back home
10-6-2020 8:30 am leave for class
10-6-2020 9:00 am arrive on campus
10-6-2020 12:00 pm leave campus
10-6-2020 12:30 pm arrive back home
10-6-2020 1:00 pm Meeting with advisors

So lets say the user inputs '10-06-2020 10:00 am Walk into class'
I want the code to insert that line between 9:00 am and 12:00 pm

I apologize if the first question i posted was incomplete.

Edit 2:
My question isn't for others to code for me. My question is can anyone point me in the direction of how to append a string in the correct position of the txt file. (If you're confused look above). Again I'm not asking anyone to code it for me.

      System.out.print("Enter the date of the task: ");
// //     STRING task_time = INPUT
      task_date = scan.nextLine();
// //     PRINT "Enter the name of the task: "
      System.out.print("Enter the time of the task: ");
// //     STRING task_name = INPUT
      String task_time = scan.nextLine();
// //     CREATE new_task = task_date + task_time + task_name (STRING)
      // String new_task = task_date;
      System.out.print("Enter the name of the task: ");
      // STRING task_date = INPUT
      String task_name = scan.nextLine();
      String task = task_date + " " + task_time  + " " + task_name;
      System.out.println(task);
//     LOADFILE tasks.txt
      String search_date = task_date;
      String search_time = task_time;
      int lineNumber = 0;
      File taskstxt = new File("tasks.txt");
      Scanner filereader = new Scanner(taskstxt);
//     SEARCH tasks.txt for corresponding date and time
      while (filereader.hasNextLine()) {
        final String lineFromFile = filereader.nextLine();
        if (lineFromFile.contains(search_date)) {

        }
      }

above is the code snippet in question. I want to search the txt file for for the date and time inputted by the user, and if it exists print "you have a task for that time already", if it doesn't exist I want to append the file with the task date/time/name. I'm sorry if this is confusing for anyone (I find it hard sometimes to write out exactly what I'm thinking)

答案1

得分: 2

Q: 如何在Java中构建“日历应用”?

A: 根据您上面的设计,您的应用将包括以下内容:

  1. 一个用于添加记录的用户界面(当前为命令行界面,但您可能要考虑使用Web界面、Android、Swing或其他替代方案)
  2. 一个用于显示记录的用户界面
  3. 一个用于保存记录的数据存储(当前为文本文件,但您可能要考虑使用类似JSON或XML的“结构化文本”格式,或者使用数据库)。

Q: 如何将新的日历记录保存在正确的位置?

A: 我建议:

  1. 编写一个名为“TaskList”的类,用于保存您的日历记录。

    简单示例:

    public class TaskList {
      public List<TaskRecord> tasks;
    
      public void save(String filename) { ... }
      public void load(String filename) { ... }
    }
    
  2. 编写一个名为“TaskRecord”的类,用于表示单个任务:

    简单示例:

    public class TaskRecord {
      public Date dateTime;
      public String taskText;
    }
    
  3. 在类中将“日期”与“文本”分开是很重要的。

    如果您想要一个简单的文本文件,并且希望所有内容都在同一行上,那么选择一个易于解析的分隔符。例如,您可以使用制表符(\t)来分隔“日期”和“时间”:

    10-6-2020 6:30 am\t起床

  4. 在您准备“保存”更新后的文件之前,“插入记录到正确位置”可能并不重要。您可以在“写入”之前简单地进行排序(按日期/时间排序):

    简单示例:

    public class TaskRecord implements Comparator<Date> {
      public Date dateTime;
      public String taskText;
    
      @Override
      public int compare(TaskRecord a, TaskRecord b) {
        return a.dateTime.compareTo(b.dateTime);
      }
    }
    
  5. 如果您真的想在插入新记录时对其进行排序,您可能仍然需要实现“Comparator”(如上所示)。您可以循环遍历列表,直到找到一个时间/日期 >= 您的新日期,然后在该索引处使用List.add()

  6. 您可能还会希望在“TaskList”类中添加用于添加、修改和/或删除记录的方法。

  7. 封装 可以使您的类更健壮。您希望从该类的用户那里隐藏与该类不相关的一切内容。

    简单示例:

    public class TaskRecord implements Comparator<Date> {
      private Date dateTime;
      private String taskText;
    
      // 在构造函数中初始化对象状态
      public TaskRecord(Date dateTime, String taskText) {
        this.dateTime = dateTime;
        this.taskText = taskText;
      }
    
      // 用于只读访问的“Getter”方法
      Date getDateTime() { return dateTime; }
      String getTaskText() { return taskText; }
    
      @Override
      public int compare(TaskRecord a, TaskRecord b) {
        return a.dateTime.compareTo(b.dateTime);
      }
    
    }
    

希望这能至少在一定程度上帮助您。

英文:

Q: How do a build a "Calendar app" in Java?

A: Per your design above, your app will have the following:

  1. A UI for adding records (currently command line, but you might want to consider a web UI, Android, Swing or other alternatives)
  2. A UI for displaying records
  3. A data store for saving records (currently a text file, but you might want to consider a "structured text" format like JSON or XML, or a database).

Q: How do I save a new calendar record in the correct position?

A: I would suggest:

  1. Write a "TaskList" class for holding your calendar records.

    SIMPLE EXAMPLE:

    public class TaskList {
      public List&lt;TaskRecord&gt; tasks;
    
      public void save(string filename) { ... }
      public void load(string filename) { ... }
    }
    
  2. Write a "TaskRecord" class for individual tasks:

    SIMPLE EXAMPLE:

    public class TaskRecord {
      public Date dateTime;
      public String taskText;
    }
    
  3. It's important to keep the "date" separate from your "text" in your class.

    If you want a simple text file, and you want everything on the same line, then choose a delimiter that makes it easy to parse. For example, you can separate "date" and "time" with a tab (\t):

    10-6-2020 6:30 am\tWake up

  4. It might be unimportant to "insert the record in the right place" until you're ready to "save" the updated file. You can simply "sort()" (by date/time) before you "write()":

    SIMPLE EXAMPLE:

     public class TaskRecord implements  Comparator&lt;Date&gt; {
       public Date dateTime;
       public String taskText;
    
       @Override
       public int compare(TaskRecord a, TaskRecord b) {
         return a.datetime.compareTo(b.datetime);
       }
     }
    
  5. If you really wanted to sort each new record as you insert it, you'd still probably want to implement "Comparator" (as above). You'd loop through your list until you find a time/date >= your new date, then List.add() at that index.

  6. You'd probably also want to add methods to your "TaskList" class to add, modify and/or delete records.

  7. "Encapsulation" can make your classes more robust. You want to hide everything about the class that's not "essential" from users of that class

    SIMPLE EXAMPLE:

       public class TaskRecord implements  Comparator&lt;Date&gt; {
         private Date dateTime;
         private String taskText;
    
         // Initialize object state in constructor
         public TaskRecord(Date dateTime, String taskText) {
           this.dateTime = dateTime;
           this.taskText = taskText;
         }
    
         // &quot;Getter&quot; methods for read-only access
         Date getDateTime() { return dateTime; }
         String getTaskText() { return taskText; }
    
         @Override
         public int compare(TaskRecord a, TaskRecord b) {
           return a.datetime.compareTo(b.datetime);
         }
    
       }
    

'Hope that helps ... at least a little


huangapple
  • 本文由 发表于 2020年10月11日 09:16:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64299797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定