Perform SQL Sorting in SPL

Question

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 number of lines is not specified and can vary; therefore I am using an Array list.

I can read in the lines and I get 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 problem is getting the program to sort by 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)

 

Answer

Sorting data by a field is a simple SQL query. As Java lacks related class library, we have to hardcode it. It’s easy to get it done in SPL (Structured Process Language). The SPL script can be regarded as a class library for Java.

First you need to define two parameters.

undefined

SPL script:

A

1

=file(argFile).import@t()

2

=A1.sort(${argFiled})

A1: Read in the file through parameter argFile, which is the text file name. For example, if parameter value is d:\\test.txt, A1 is =file(“d:\\test.txt”).import@t().

A2: Sort data by the specified field. Here the macro replacement (${argFiled}) is used to specify a sorting field dynamically. For example, if the parameter value is firstname, A2 is =A1.sort(firstname). Macro replacement can be used in functions such as group, groupx, and sortx to perform a SQL grouping and aggregation or sorting.

To call an SPL script in a Java application, see How to Call an SPL Script in Java.