针对结构化文本的排序计算
【问题】
What I am trying to do exactly is creating a sorting program that sorts PatientRecords by whatever the user specifies in the command-line.
The program is operated on command-line and the user will input a text file containing the records as the first argument (args[0]), and how he wants it sorted as the second argument(args[1]).
The text file is formatted as: Lastname, Firstname, Age, Roomnumber for each line.
The amount of Lines is not specified and can vary, therefore I am using an Array list.
I can read in the lines and I got to where I could sort it by last name, but it looks like to me that the only way to do it is by separating the line at the commas and apprehending them individually in separate methods.
If there is a better way please let me know, I am open to anything. My main problem is getting the program to sort by the different categories, such as Age or RoomNumber.
Here is my code:
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)
【回答】
按列名排序是简单的SQL式计算,但JAVA缺乏相关的类库,自行实现比较麻烦。用SPL作为类库辅助JAVA来作要简单得多。
首先定义好参数
脚本如下:
A |
|
1 |
=file(argFile).import@t() |
2 |
=A1.sort(${argFiled}) |
A1:读取文本,argFile为参数,参数值为文本文件名
比如参数值为d:\\test.txt,则A1就等同于=file(“d:\\test.txt”).import@t()
A2:按照指定的字段分组,利用宏替换${argFiled}实现动态指定排序字段
比如参数值为firstname,则A2就等同于=A1.sort(firstname)
同理,我们还可以把宏替换运用于group,groupx,sortx等等函数,协助java处理结构化文本的分组汇总,排序等SQL式计算。
写好的脚本如何在应用程序中调用,可以参考Java 如何调用 SPL 脚本