【问题标题】:How do I read in comma separated string from text file that is inputted from command-line as argument?如何从命令行输入的文本文件中读取逗号分隔的字符串作为参数?
【发布时间】:2015-05-23 09:46:45
【问题描述】:

我正在编写一个对医院记录进行分类的程序。这些记录出现在一个文本文件中,用户在调用下面的“Patient”类时在命令行中输入该文件作为参数。文本文件中每一行的记录格式为 lastname(string)、firstname(string)、roomnumber(int)、age(int)。行数未知。用户将指定文件名作为第一个参数,然后指定要排序的字段。 我特别难以弄清楚的是如何读取文本文件并将信息存储在数组中。到目前为止,我已经坚持了大约一个星期,所以我从头开始了几次。这是我到目前为止所拥有的。

import java.util.*;
import java.io.*;

public class Patient
{
        public static void main(String args[])
    {
        System.out.println("Servando Hernandez");
        System.out.println("Patient sorting Program.");

        Scanner scan = new Scanner(args[0]);
        String[] Rec = new String[10];
        while(scan.hasNextLine)
        {

          scan.nextLine = Rec[i];


        }
        Arrays.sort(Rec);
        for(int j=0; j<Rec.length; j++)
        {
            System.out.println(Rec[j]);
        }
    }
}

【问题讨论】:

  • 必须是数组吗?使用某种List 会更容易。此外,您可能会考虑创建一个“记录” POJO,其中包含每行的信息,再次,这将使事情变得更容易
  • 好的,是的!我只需要一些想法,因为我只是坚持阅读这些内容。它不必是一个数组。我只是认为这可能是我需要做的。

标签: java arrays command-line arguments


【解决方案1】:

行数未知

这表明您需要一个可以增长以满足您的需求的动态数据结构。考虑使用某种List

List<String> lines = new ArrayList<>(10);
while(scan.hasNextLine)
{
    lines.add(scan.nextLine());
}

查看Collections Trail了解更多详情

因为您正在处理结构化数据,我会考虑创建某种 POJO 以使其更易于管理...

public class PatientRecord {
    private final String firstName;
    private final String lastName;
    private final int roomNumber;
    private final int age;

    public PatientRecord(String firstName, String lastName, int roomNumber, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.roomNumber = roomNumber;
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getRoomNumber() {
        return roomNumber;
    }

    public int getAge() {
        return age;
    }


}

然后,当您从文件中读取该行时,您将对其进行解析,创建一个 PatientRecord 的新实例并将其添加到您的 List 中。

这意味着您可以使用 Collections.sort 之类的东西根据您的需要对 List 进行排序

【讨论】:

  • 感谢您的回复。我将阅读更多关于 POJO 的内容,因为它对我来说有点新鲜,但这一切对我帮助很大。
【解决方案2】:

现在,我不太确定这是否正是您想要的,因为您给我的代码并不多,但这是您的相同代码,已修复、略微重构,并设置为返回行已读入,但按姓氏排序:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class Patient {

    public static void main(String args[]) {
        System.out.println("Servando Hernandez");
        System.out.println("Patient sorting Program.");

        Scanner scan = null;
        try {
            scan = new Scanner(new File(args[0]));
        } catch (FileNotFoundException e) {
            System.err.println("File path \"" + args[0] + "\" not found.");
            System.exit(0);
        }

        ArrayList<String> lines=new ArrayList<String>();
        while(scan.hasNextLine())
            lines.add(scan.nextLine());

        Collections.sort(lines);

        for(String x : lines)
            System.out.println(x);
    }
}

希望对您有所帮助,祝您好运。

【讨论】:

  • 感谢您的快速回复。我之前尝试过 try catch,但语法错误并使用 FileReader 而不是 File。现在我需要做的就是研究一下如何在逗号处分隔行,以便能够按照所需的标准对它们进行排序。
猜你喜欢
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
相关资源
最近更新 更多