将文本文件中的字符串解析为日期
我正在尝试从文本文件中读取日期,然后将字符串解析为日期,以便我可以将其读入数组中。当我尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在捕获 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.