将数组列表格式化为特定格式

时间:2015-03-20 22:40:42

标签: java arrays sorting arraylist

我正在编写的程序将对文本文件中的医院记录进行排序。文本文件的格式是lastname,firstname,age,roomnumber,它看起来完全像这样:

Franklin,Benjamin,74,18
Hamilton,Alexander,25,6
Thatcher,Margaret,65,3
Nixon,Richard,45,7

并且必须以特定格式打印,但用户将指定它们的排序方式。按姓氏排序时格式如下:

Last     First     Age      Room
Coolidge Calvin     24        27
Franklin Benjamin   74         8
Hamilton Alexander  50       123
Nixon    Richard    45         7

我一直试图找到存储线路的方法,但仍然可以将线路打印在一起以便将信息保存在一起。 必须通过命令行调用程序,同时调用程序,用户必须将输入文件指定为第一个参数(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();
//          }
//      }
//
        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;
}

2 个答案:

答案 0 :(得分:1)

你应该写一个自定义比较器,让我们称之为PatientComparator。我不会完全实施比较方法,这是你的工作: - )

class PatientComparator implements Comparator<Patient> {
    String sortType;
    public PatientComparator(String sortType) {
        this.sortType = sortType;
    }

    @Override
    public int compare(Patient a, Patient b) {
        // TODO: write your switch case here
        return a.firstName.compareTo(b.firstName);
    }

然后,您可以使用自己的比较器对患者进行排序:

Collections.sort(patients, new PatientComparator(arg[1]));

答案 1 :(得分:0)

好吧,我首先将你的练习分为两部分:

1-通过命令行使用args [0]调用程序。浏览文件并向用户显示内容(通过尊重您的格式)。

2-为脚本添加排序(这可以通过多种方式完成;通常通过覆盖compare方法)。执行步骤1,对arg [1]进行排序,然后输出。

为了让您入门,我已经为您完成了第1步,而没有对输出进行任何格式化。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Test3 {
    //You will store each line as an array of strings
    //then you store the lines in a list of arrays of strings
    public static ArrayList<String[]> mainList=new ArrayList<String[]>();
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new FileReader(args[0]));
        try {
           //reading each line from the input file
            String line = br.readLine();
           //spliting the line by comma (,) which will return 
           //an array of strings - our names and numbers in string format
            while (line != null) {
                String[] lineElements=line.split(",");
                line = br.readLine();
                mainList.add(lineElements);
            }

        } finally {
            br.close();
        }
        //showing to the user
        for (String[] line :mainList){
            for (String x:line){
                System.out.print(x+" ");
            }
            System.out.println("");
        }

    }

}

您只需编译:{{1​​}}

然后,您只需在实际运行时指定文件的位置:javac name.java

我使用你的示例文件运行它,我在控制台中得到它(你只需要格式化输出):

java name /path/to/your/file/

还有很多工作要做,但我认为这是一个很好的开始!

相关问题