0

Given a list of patient demographics for several patients, we want to be able to group the data by patients with the same name. The demographics provided are (in order): Patient ID, Patient Name, Patient Sex, and Patient Date Of Birth. For example, here's a sample input:

PID1,POND^AMY,F,19890224

PID2,WILLIAMS^RORY,M,19881102

PID3,POND^AMY,F,19890224

PID4,CLARA^OSWALD,F,19890224

PID5,POND^AMY,F,20010911

PID6,CLAR^OSWALD,F,19890224

PID7,POND^AMELIA,F,20010911

PID8,CLARA^oswald,F,19890224

PID9,TYLER^ROSE,F,20000101

PID10,NOBLE^DONNA,F,19780405

PID11,TYLER^ROSE,F,20000101

PID12,NOBLE^DONN,F,19780405

PID13,TYLER^ROSE,F,20000102

PID14,CLARA^OSWALD^COLEMAN,F,19890224

The expected output is:

0:

PID1,POND^AMY,F,19890224

PID3,POND^AMY,F,19890224

PID5,POND^AMY,F,20010911

1:

PID2,WILLIAMS^RORY,M,19881102

2:

PID4,CLARA^OSWALD,F,19890224

PID8,CLARA^oswald,F,19890224

PID14,CLARA^OSWALD^COLEMAN,F,19890224

3:

PID6,CLAR^OSWALD,F,19890224

4:

PID7,POND^AMELIA,F,20010911

5:

PID9,TYLER^ROSE,F,20000101

PID11,TYLER^ROSE,F,20000101

PID13,TYLER^ROSE,F,20000102

6:

PID10,NOBLE^DONNA,F,19780405

7:

PID12,NOBLE^DONN,F,19780405

I will attempt this code:

import java.io.bufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.printWriter;
import java.util.ArrayList;
import java.util.Collection;
public class FileReaderWriter

{
public static void main(String[]args) throws IOException
{
    BufferedReader reader = null ;
    PrinterWriter outputStream = null;
    ArrayList<String> rows = new ArrayList<String>();       
    try{
        reader = new BufferedReader( new FileReader("Input.txt"));
        outputstream = new PrintWriter(new FileWriter("Output.txt"));
        String file;
        while ((file = reader.readLine())!= null){
            rows.add(file);
        }
        Collections.sort(rows);
        String[] strArr = rows.toArray(new String[0]);
        for(String cur : strArr);
        outputStream.println(cur);
    } finally {
        if(reader != null){
            reader.closr();
        }
        if(outputStream != null){
            outputStream.close();
        }
    }
}
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59
Nayan
  • 1
  • 1
  • 1
  • 1
    And what did you attempt? Where exactly are you facing problems? SO is not code writing service... – Chaosfire May 20 '22 at 15:53
  • Please read [how to ask](https://stackoverflow.com/help/how-to-ask), the [FAQ](https://meta.stackoverflow.com/questions/251225/faq-index-for-stack-overflow) as well. – Chaosfire May 20 '22 at 15:56
  • 1
    I would suggest creating a `Record` or `POJO`. Read the data into the `Record` or `POJO` and use a `Comparator` to sort the data. After sorting, write the data to a new file. https://stackoverflow.com/questions/12542185/sort-a-java-collection-object-based-on-one-field-in-it – SedJ601 May 20 '22 at 16:47
  • Something else I would do is create a `HashMap` of `String` - `List`. For the key, use the name. Add the line to the value `List`. Sort the `HashMap` by key. Loop through the sorted `HashMap` and loop through `List` to write the data to a new file. – SedJ601 May 20 '22 at 16:55
  • I left out an important part in my previous comment. -> Read the data into the `Record` or `POJO`. Add the `Records`/`POJOs` into a `List`. Use the `Comparator` to sort the `List`. – SedJ601 May 20 '22 at 16:59
  • No doubt that these are patients, since they've certainly all seen The Doctor! :) – David Conrad May 20 '22 at 18:55

0 Answers0