将文本文件中的字符串解析为日期

发布于 2024-12-05 05:43:12 字数 1915 浏览 0 评论 0原文

我正在尝试从文本文件中读取日期,然后将字符串解析为日期,以便我可以将其读入数组中。当我尝试 Date date = sdf.parse (token.nextToken ()); 时,我不断收到错误。我该如何将从文本文件中读取的字符串转换为日期?

日期设置

void setDate(Date d)
{
date =d;
}

文件阅读器

void read ()
    {
        SimpleDateFormat sdf = new SimpleDateFormat ("dd/MM/yyyy");
        int cnt = 0;
        try
        {
            FileReader fr = new FileReader ("oefeningtaak.txt");
            BufferedReader br = new BufferedReader (fr);
            boolean canread = true;
            while (canread == true)
            {
                String lyn = br.readLine ();
                if (lyn == null)
                {
                    canread = false;
                }
                else
                {
                    array [cnt] = new Mainobject ();
                    StringTokenizer token = new StringTokenizer (lyn, "*");
                    String students = token.nextToken ();
                    String vak = token.nextToken ();
                    String maxpunt = token.nextToken ();
                    String punt = token.nextToken ();
                    Date date = sdf.parse (token.nextToken ());
                    array [cnt].setStudents (students);
                    array [cnt].setVak (vak);
                    array [cnt].setMaxpunt (Integer.parseInt (maxpunt));
                    array [cnt].setPunt (Integer.parseInt (punt));
                    array [cnt].setDate (date);
                    array [cnt].report ();
                    cnt++;
                }
            }
        }
        catch (IOException err)
        {
            System.out.println (err.toString ());

        }
    }

我得到的错误是

方法“java.util.Date parse(java.lang.String $1) throws java.text.ParseException:”可以抛出检查异常“java.text.ParseException”。因此它的调用必须包含在捕获异常的 try 语句中,否则必须声明此方法抛出异常

I'm trying to read a date out of a text file then parse the String into a date so I can read it into my array. I keep getting an error when I try Date date = sdf.parse (token.nextToken ());. What can I do to convert the string I read from my text file into a date?

Date set

void setDate(Date d)
{
date =d;
}

File Reader

void read ()
    {
        SimpleDateFormat sdf = new SimpleDateFormat ("dd/MM/yyyy");
        int cnt = 0;
        try
        {
            FileReader fr = new FileReader ("oefeningtaak.txt");
            BufferedReader br = new BufferedReader (fr);
            boolean canread = true;
            while (canread == true)
            {
                String lyn = br.readLine ();
                if (lyn == null)
                {
                    canread = false;
                }
                else
                {
                    array [cnt] = new Mainobject ();
                    StringTokenizer token = new StringTokenizer (lyn, "*");
                    String students = token.nextToken ();
                    String vak = token.nextToken ();
                    String maxpunt = token.nextToken ();
                    String punt = token.nextToken ();
                    Date date = sdf.parse (token.nextToken ());
                    array [cnt].setStudents (students);
                    array [cnt].setVak (vak);
                    array [cnt].setMaxpunt (Integer.parseInt (maxpunt));
                    array [cnt].setPunt (Integer.parseInt (punt));
                    array [cnt].setDate (date);
                    array [cnt].report ();
                    cnt++;
                }
            }
        }
        catch (IOException err)
        {
            System.out.println (err.toString ());

        }
    }

The error I get is

The method "java.util.Date parse(java.lang.String $1) throws java.text.ParseException: "can throw the checked exception "java.text.ParseException". so its invocation must be enclosed in a try statement that catches the exception, or else this method must be declared to throw the exception

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2024-12-12 05:43:12

您正在捕获 IOException,但没有捕获或声明 ParseException。

您需要捕获它或声明您的方法可能会抛出它,因为它是受检查的异常。

You are catching an IOException, but you are not catching, or declaring, the ParseException.

You need to either catch it or declare that your method may throw it, as it is a checked exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文