我想做的是创建一个排序程序,根据用户在命令行中指定的任何内容对 PatientRecords 进行排序。
该程序在命令行上运行,用户将输入一个文本文件,其中包含作为第一个参数(args[0])的记录,以及他希望如何排序作为第二个参数(args[1])。
文本文件的格式为:每行姓氏、名字、年龄、房间号
。
行数未指定并且可能会有所不同,因此我使用数组列表。
我可以阅读行中的内容,并且到达可以按姓氏对其进行排序的位置,但在我看来,唯一的方法是用逗号分隔行并用单独的方法单独理解它们.
如果有更好的方法请告诉我,我愿意接受任何事情。我的主要问题是让程序按不同类别排序,例如年龄或房间编号。
这是我的代码:
import java.io.*;
import java.util.*;
public class PatientRecord
{
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());
if(!(args.length == 0))
{
if(args[1] == lastname)
{
sortByLastName();
}
else if(args[1] == firstname)
{
sortByLastName();
}
else if(args[1] == age)
{
sortByAge();
}
else if(args[1] == roomnumber)
{
sortByRoomNumber();
}
}
}
static String sortByLastName()
{
Collections.sort(lines);
for(String x : lines)
System.out.println(x);
}
static String sortByFirstName()
{
}
static int sortByAge()
{
}
static int sortByRoomNumber()
{
}
}
最佳答案
创建一个名为
Patient
的模型类,其中包含名字、姓氏等。class Patient{ String firstName; String lastName; // Constructor, getter, setter }
我猜,文本文件行是用逗号分隔的。因此,将行拆分为数组并填充列表
List<Parent> patients= new ArrayList<>(); while(sc.hanNextLine()){ String[] values= sc.nextLine().split(","); patients.add(new Patient(...)) }
现在,从命令行读取客户偏好并对患者列表进行排序。
String sortType= sc.next() switch(sortType)){//Use java 7 or greater for string switch case "firsname": //Now sort the list by firstname using Comparator sort method. break; case "lastname": .... }
关于java - 在数组列表中分隔逗号分隔的值,然后将其放回一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29172183/