【问题标题】:Extracting data from a text file in order to make a Gregorian Calender从文本文件中提取数据以制作公历
【发布时间】:2013-04-19 05:10:14
【问题描述】:

我在网上上 java 课,正在为一项作业苦苦挣扎 “创建一个名为 dteDate 的 GregorianCalendar 对象。对于年、月、日、小时、分钟和秒,使用 parseInt() 从文件中将这些数字提取到单独的整数变量中。然后创建您的 GregorianCalendar 对象。最后,使用 SimpleDateFormat 创建一个名为strDate 包含“2013 年 4 月 28 日 12:41”。换句话说,我希望看到您了解如何从文件中提取一系列字符串,将它们转换为整数,创建一个 GregorianCalendar 对象,然后将其转换回格式化字符串。当然不是最有效的处理方式,但它会测试您创建日期变量的能力。"
我不知道如何从文本文件中提取特定行,以及如何提取整个内容,并希望我的程序看起来像这样的指导。

import java.io.*;
import java.nio.file.*;
import java.util.GregorianCalendar;
public class a19010
{

public static void main(String[] args) 
  {
    Path objPath = Paths.get("dirsize.txt");  
    if (Files.exists(objPath))

    {
        File objFile = objPath.toFile();
        try(BufferedReader in = new BufferedReader(
                                new FileReader(objFile)))
        {
            String line = in.readLine();

            while(line != null)
            {
                System.out.println(line);
                line = in.readLine();
            }

        }    
      catch (IOException e)
      {
        System.out.println(e);  
      }
    }
    else
    {
     System.out.println(
             objPath.toAbsolutePath() + " doesn't exists");   
    }   

    GregorianCalendar dteDate = new GregorianCalendar();
  }
}

这会将整个文本文件打印到控制台,这是 assinment 的第一步,然后我创建了 GregorianCalendar dteDate。我不确定如何提取需要为我的日历提供相关信息的行以及在此程序中该做什么。我不想在这篇文章中说太多话,让我的问题更难回答,所以我会从文本文件中发布相关行,如果我需要在我的问题中添加更多信息或取出一些信息或澄清让我知道我会编辑它。 文本文件中带有日期和时间的行是其中的前4行,如下所示:(放入类似代码以使其清楚)

The current date is: Thu 03/28/2013    
Enter the new date: (mm-dd-yy)  
The current time is: 12:41:26.31
Enter the new time:  

如果有人知道该怎么做,或者甚至可以为我指明正确的方向,我将不胜感激。

这是我在 Bhushan Bhangale 的帮助下获得的代码,但我无法导入到我的日历中。

String line = in.readLine();
        int lineNum = 1;
        //extract the month
        String monthSubstring = line.substring(25,27);
        int month = Integer.parseInt(monthSubstring) -1 ;
        //extact the year
        String yearSubstring = line.substring(31,35);
        int year = (Integer.parseInt(yearSubstring));
        //extract the date
        String daySubstring = line.substring(28,30);
        int day = Integer.parseInt(daySubstring);

        while(line != null)
        {
            System.out.println(line);
            line = in.readLine();
            lineNum ++;

         if (lineNum == 3)
         {
           //extract the hour
            String hourSubstring = line.substring(21,23);
            int hour = Integer.parseInt(hourSubstring); 
            //extract the minute
            String minuteSubstring = line.substring(24,26);
            int minute = Integer.parseInt(minuteSubstring);
            //extract the second
            String secondSubstring = line.substring(27,29);
            int second= Integer.parseInt(secondSubstring);
           }
       } 
     GregorianCalendar dteDate = new GregorianCalendar(year, month, day, hour, minute, second);
        System.out.println(dteDate.getTime());  

问题是日历看不到小时分钟或秒,我不知道如何让它导入它们

【问题讨论】:

    标签: java text-files text-extraction gregorian-calendar


    【解决方案1】:

    由于它的课堂作业,我会为你提供进一步的提示。

    首先让我们假设文件格式是固定的。

    第一行是日期,第三行是时间。在 while 循环中,由于您是逐行阅读,因此您肯定可以处理哪一行是第一行或第三行。

    然后从第一行使用 regex (preferable) 或 StringTokenizer 取出日期、月份和年份。忽略这一天,因为它无关紧要。

    然后从第三行再次使用 regex 或 tokenizer 取出小时、分钟和秒。

    检查 GregorianCalendar api 以设置此信息并创建对象。

    使用 SimpleDateFormat 定义所需的日期格式并传递日期以获取格式化日期。

    【讨论】:

    • 感谢您的帮助,我的困惑来自 while 循环中的步骤,因为它循环遍历所有行我如何让它停在特定行以便访问它,例如第一行或第三个?
    • 在 while 循环之前创建 GregorianCalendar 对象。在 while 循环中,当您阅读第一行调用方法时说 parseDate(calendar, line) 并将日历和行传递给该方法。同样,当您找到第三行调用方法 parseTime(calendar, line) 时。在这些方法中,根据需要解析字符串并设置日历属性。在 while 循环之后,您的日历将设置为读取信息。
    • 我想出了如何做第一行,但我遇到的问题是将 while 循环作为特定行停止,以便实现一种方法,我只能做第一个,因为它在 while 之外循环我想出了一个新方法,但我觉得它不是正确的编码形式,它不能可靠地处理不同的数据我把它编辑到我的顶帖中,这样你就可以看到它
    • Dude 启动一个计数器,并在每行读取后递增计数器。添加一个 if 或 switch 子句来检查计数器何时为 1 调用 parseDate() 方法以及何时其 3 调用 parseTime() 方法。阅读 counter、if 和 switch 以了解如何操作。一旦你通过你理解的任何东西实现了所有这些,以后就可以改进代码。
    • 好吧,所以在 while 循环之前定义你的小时、分钟、秒变量,你应该没问题。如果您在 {} 中定义了一个变量,那么它只能在其中使用,称为作用域。
    猜你喜欢
    • 2020-07-21
    • 2020-12-20
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多