从文件中读取数据并解析
我尝试从这个txt文件中读取数据 所以稍后我可以获得例如最大的 ID 或最近的访问日期等:
`Id_doctor Id_patient Date_visit
23 124 2006-12-13
23 172 2006-1-25
23 191 2006-2-1
23 191 2006-9-26
23 207 2006-9-25
23 252 2006-3-6
23 272 2006-10-26
23 272 2007-2-13
23 291 2006-1-9
23 291 2006-6-6
23 416 2006-1-7
25 100 2006-8-4
25 135 2006-9-24
25 135 2007-4-7
25 248 2006-5-26
26 238 2006-8-2
26 238 2007-3-12
26 249 2006-10-19
26 401 2006-4-9
28 111 2006-2-3
28 161 2006-10-15
28 209 2007-4-1
28 246 2006-7-17
28 246 2006-9-27
`
这是我为 txt 文件创建的类:
import java.time.LocalDate;
public class Visit {
private int doctorId;
private int patientId;
private LocalDate visitDate;
public Visit(int doctorId, int patientId, LocalDate visitDate) {
this.doctorId = doctorId;
this.patientId = patientId;
this.visitDate = visitDate;
}
public int getDoctorId() {
return doctorId;
}
public void setDoctorId(int doctorId) {
this.doctorId = doctorId;
}
public int getPatientId() {
return patientId;
}
public void setPatientId(int patientId) {
this.patientId = patientId;
}
public LocalDate getVisitDate() {
return visitDate;
}
public void setVisitDate(LocalDate visitDate) {
this.visitDate = visitDate;
}
@Override
public String toString() {
return "Visit{" +
"doctorId=" + doctorId +
", patientId=" + patientId +
", visitDate=" + visitDate +
'}';
}
}
这是我读取文件并解析它的代码 这样我就可以提取我需要的内容:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class Main4 {
public static void main(String[] args) throws IOException {
List<Visit> visits = readAndParse();
System.out.println(visits);
}
public static List<Visit> readAndParse() throws IOException {
List<Visit> visits = new ArrayList<>();
BufferedReader bufferedReader = new BufferedReader(new FileReader("wizyty.txt"));
bufferedReader.readLine();
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] s = line.split(" \\s");
visits.add(new Visit(Integer.parseInt(s[0]), Integer.parseInt(s[1]), LocalDate.parse(s[2])));
}
bufferedReader.close();
return visits;
}
}
当我尝试运行它时,我得到以下输出:
Exception in thread "main" java.lang.NumberFormatException: For input string: "23 124 2006-12-13"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at victor.Main4.readAndParse(Main4.java:31)
at victor.Main4.main(Main4.java:16)
有没有人可以向我解释我做错了什么? 谢谢
I try to read the data from this file txt
so later on I can get for example the bigest id or recent date of visit and so on :
`Id_doctor Id_patient Date_visit
23 124 2006-12-13
23 172 2006-1-25
23 191 2006-2-1
23 191 2006-9-26
23 207 2006-9-25
23 252 2006-3-6
23 272 2006-10-26
23 272 2007-2-13
23 291 2006-1-9
23 291 2006-6-6
23 416 2006-1-7
25 100 2006-8-4
25 135 2006-9-24
25 135 2007-4-7
25 248 2006-5-26
26 238 2006-8-2
26 238 2007-3-12
26 249 2006-10-19
26 401 2006-4-9
28 111 2006-2-3
28 161 2006-10-15
28 209 2007-4-1
28 246 2006-7-17
28 246 2006-9-27
`
This is the class that i create for the txt file :
import java.time.LocalDate;
public class Visit {
private int doctorId;
private int patientId;
private LocalDate visitDate;
public Visit(int doctorId, int patientId, LocalDate visitDate) {
this.doctorId = doctorId;
this.patientId = patientId;
this.visitDate = visitDate;
}
public int getDoctorId() {
return doctorId;
}
public void setDoctorId(int doctorId) {
this.doctorId = doctorId;
}
public int getPatientId() {
return patientId;
}
public void setPatientId(int patientId) {
this.patientId = patientId;
}
public LocalDate getVisitDate() {
return visitDate;
}
public void setVisitDate(LocalDate visitDate) {
this.visitDate = visitDate;
}
@Override
public String toString() {
return "Visit{" +
"doctorId=" + doctorId +
", patientId=" + patientId +
", visitDate=" + visitDate +
'}';
}
}
here is my code where I read the file and parse it
so I can extrat what I need to after :
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class Main4 {
public static void main(String[] args) throws IOException {
List<Visit> visits = readAndParse();
System.out.println(visits);
}
public static List<Visit> readAndParse() throws IOException {
List<Visit> visits = new ArrayList<>();
BufferedReader bufferedReader = new BufferedReader(new FileReader("wizyty.txt"));
bufferedReader.readLine();
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] s = line.split(" \\s");
visits.add(new Visit(Integer.parseInt(s[0]), Integer.parseInt(s[1]), LocalDate.parse(s[2])));
}
bufferedReader.close();
return visits;
}
}
When I try to rune it I get the following output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "23 124 2006-12-13"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at victor.Main4.readAndParse(Main4.java:31)
at victor.Main4.main(Main4.java:16)
Is there anyone that can explan to me what I do wrong ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该解决方案已被修改以适应您的限制,即无论如何都不修改文件,因此我用您提供的旧内容替换了文本文件的内容,并修改了程序以按原样使用文件!
Main4.java
访问
.Java文本文件返回到init状态
The solution has been modified to fit your constraints which is to not modify the file anyhow so I replaced the contents of the text file with the old ones which you've provided and modified your program to work with the file as it is!
Main4.java
Visits.Java
Text file returned to init state