java - Formatting an array list into a specific format -


the program writing sort hospital record comes in text file. format of text file lastname,firstname,age,roomnumber, :

franklin,benjamin,74,18 hamilton,alexander,25,6 thatcher,margaret,65,3 nixon,richard,45,7 

and has printed format, user specify how sorted. format when sorted last name:

last     first     age      room coolidge calvin     24        27 franklin benjamin   74         8 hamilton alexander  50       123 nixon    richard    45         7 

i have been stuck on trying find way store lines , still able print out lines in order keep information together. program has called through command-line , @ same time program called user must specify input file first argument (args[0]) , how sort second argument (args[1]). have tried few different ways keep getting stuck in same place, best way approach this? current code btw. , comment blocks old code have tried , keep around, in case.

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(); //          } //      } //         list<patient> patients = new arraylist<>();             while(scan.hasnextline())             {                 string[] values= scan.nextline().split(",");                 patients.add(new patient())             }             string sorttype= args[1]              switch(sorttype))             {                 case "firsname":                  break;                 case "lastname":                 break;                 case "age":                 break;                 case "roomnumber":                 break;             }     } //  static string sortbylastname() //  { //       collections.sort(lines); // //        for(string x : lines) //            system.out.println(x); //  }   class patient {     string firstname;     string lastname;     int age;     int roomnumber; } 

you should write custom comparator, let's call patientcomparator. not implement compare method completely, that's job :-)

class patientcomparator implements comparator<patient> {     string sorttype;     public patientcomparator(string sorttype) {         this.sorttype = sorttype;     }      @override     public int compare(patient a, patient b) {         // todo: write switch case here         return a.firstname.compareto(b.firstname);     } 

then, can sort patients using own comparator:

collections.sort(patients, new patientcomparator(arg[1])); 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -