Home > Software design >  Read data from file and parse it
Read data from file and parse it

Time:02-23

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

CodePudding user response:

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

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

//Needs a parser
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 {
        int id, patientId;
        DateTimeFormatter date;
        List<Visit> visits = new ArrayList<>();
        BufferedReader bufferedReader = new BufferedReader(new FileReader("wizyty.txt"));
        bufferedReader.readLine();
        String line;
        while ((line = bufferedReader.readLine()) != null)
        {
            if(line.charAt(0) == '`') continue;
            else
            {
                String[] s = line.split("\\s");
                s = removeEmptyTokens(s);
                for (int i = 0; i < s.length; i  )
                {
                    visits.add(
                            new Visit(Integer.parseInt(s[0]),
                                    Integer.parseInt(s[1]),
                                    DateTimeFormatter.ofPattern(s[2]))
                    );
                }
            }

        }
        bufferedReader.close();
        return visits;
    }

    private static String[] removeEmptyTokens (String[] text)
    {
        ArrayList<String> newText = new ArrayList<>();
        for (int i = 0; i < text.length; i  )
        {
            if(text[i].isEmpty()) continue;
            else newText.add(text[i]);;
        }
        return replaceElements(newText, text);
    }

    private static String[] replaceElements(ArrayList<String> list, String[] array)
    {
        array = new String[list.size()];
        for (int i = 0; i < list.size();   i)
        {
            array[i] = list.get(i);
        }
        return array;
    }
}

Visits.Java

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Visit
{
    private int doctorId;
    private int patientId;
    private DateTimeFormatter visitDate;

    public Visit(int doctorId, int patientId, DateTimeFormatter 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 DateTimeFormatter getVisitDate() {
        return visitDate;
    }

    public void setVisitDate(DateTimeFormatter visitDate) {
        this.visitDate = visitDate;
    }

    @Override
    public String toString() {
        return "Visit{"  
                "doctorId="   doctorId  
                ", patientId="   patientId  
                ", visitDate="   visitDate  
                '}';
    }
}

Text file returned to init state

`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
`
  • Related