'Read data from file and parse it
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
