从文件中读取数据并解析

发布于 2025-01-09 04:37:54 字数 3064 浏览 0 评论 0原文

我尝试从这个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 技术交流群。

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

发布评论

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

评论(1

你列表最软的妹 2025-01-16 04:37:54

该解决方案已被修改以适应您的限制,即无论如何都不修改文件,因此我用您提供的旧内容替换了文本文件的内容,并修改了程序以按原样使用文件!

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;
    }
}

访问

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 +
                '}';
    }
}

.Java文本文件返回到init状态

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

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