Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从作为参数从命令行输入的文本文件中读取逗号分隔的字符串?_Java_Arrays_Command Line_Arguments - Fatal编程技术网

Java 如何从作为参数从命令行输入的文本文件中读取逗号分隔的字符串?

Java 如何从作为参数从命令行输入的文本文件中读取逗号分隔的字符串?,java,arrays,command-line,arguments,Java,Arrays,Command Line,Arguments,我正在写一个整理医院记录的程序。这些记录以文本文件的形式出现,当用户调用下面的类“Patient”时,用户在命令行中输入该文件作为参数。文本文件中每行记录的格式为lastname(string)、firstname(string)、roomnumber(int)、age(int)。行数未知。用户将指定文件名作为第一个参数,然后指定要排序的字段。 我特别难以理解的是如何读取文本文件并将信息存储在数组中。到目前为止,我已经坚持了大约一个星期,所以我有几次从零开始。这是我到目前为止所拥有的 impor

我正在写一个整理医院记录的程序。这些记录以文本文件的形式出现,当用户调用下面的类“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]);
        }
    }
}
import java.util.*;
导入java.io.*;
公立病人
{
公共静态void main(字符串参数[])
{
System.out.println(“Servando Hernandez”);
System.out.println(“患者分类程序”);
扫描仪扫描=新扫描仪(参数[0]);
String[]Rec=新字符串[10];
while(scan.hasNextLine)
{
scan.nextLine=Rec[i];
}
数组。排序(Rec);
对于(int j=0;j
行数未知

这意味着你需要一个动态的数据结构来满足你的需求。考虑使用某种代码>列表< /C> < /P>

List<String> lines = new ArrayList<>(10);
while(scan.hasNextLine)
{
    lines.add(scan.nextLine());
}
然后,当您从文件中读取该行时,您将对其进行解析,创建一个新的
PatientRecord
实例,并将其添加到
列表中


这意味着您可以使用类似于
集合的东西。排序
根据您的需要对
列表
进行排序

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

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);
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Scanner;
公立病人{
公共静态void main(字符串参数[]){
System.out.println(“Servando Hernandez”);
System.out.println(“患者分类程序”);
扫描仪扫描=空;
试一试{
扫描=新扫描仪(新文件(args[0]);
}catch(filenotfounde异常){
System.err.println(“文件路径\”+args[0]+“\”未找到”);
系统出口(0);
}
ArrayList行=新的ArrayList();
while(scan.hasNextLine())
line.add(scan.nextLine());
集合。排序(行);
用于(字符串x:行)
系统输出println(x);
}
}

我希望这对你有帮助,祝你好运。

它必须是一个数组吗?在某种情况下,使用<代码>列表>代码会更容易。你也可以考虑创建一个“记录”。保存每行信息的POJO,同样,这会让事情变得更好。是的!我只是需要一些想法,因为我只是停留在阅读这些行上。它不必是数组。我只是认为这可能是我需要做的。谢谢你的回复。我将阅读更多关于POJO的内容,因为它对我来说是一种新的东西我,但所有这些都帮了我很大的忙。感谢您的快速响应。我以前尝试过使用catch,但语法错误,使用FileReader而不是File。现在我需要做的就是研究一下如何在逗号处分隔行,以便能够根据需要的标准对它们进行排序。
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);
    }
}