繁体   English   中英

在数组列表中分隔逗号分隔的值,然后将其放回去

[英]Separate a comma separated value in an array list then put it back together

我要尝试做的就是创建一个排序程序,该程序根据用户在命令行中指定的内容对PatientRecords进行排序。

该程序在命令行上操作,用户将输入一个包含记录的文本文件作为第一个参数(args [0]),以及他希望如何将其排序为第二个参数(args [1])。

文本文件的格式为:每行Lastname, Firstname, Age, Roomnumber ,名Lastname, Firstname, Age, Roomnumber

未指定行数,并且行数可以变化,因此我使用的是数组列表。

我可以阅读这些行,然后到达可以按姓氏对其进行排序的位置,但是在我看来,唯一的方法是将这些行分隔为逗号,然后分别使用不同的方法来理解它们。

如果有更好的方法,请告诉我,我乐于接受。 我的主要问题是让程序按不同类别(例如,年龄或房间号)进行排序。

这是我的代码:

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();
             }
         }

      }
      static String sortByLastName()
      {
          Collections.sort(lines);

         for(String x : lines)
             System.out.println(x);
      }

      static String sortByFirstName()
      {

      }

      static int sortByAge()
      {

      }

      static int sortByRoomNumber()
      {

      }
 }
  • 创建一个名为Patient的模型类,它具有firstName,lastName等。

     class Patient{ String firstName; String lastName; // Constructor, getter, setter } 
  • 我猜,文本文件行以逗号分隔。 因此,将行拆分为数组并填充列表

     List<Parent> patients= new ArrayList<>(); while(sc.hanNextLine()){ String[] values= sc.nextLine().split(","); patients.add(new Patient(...)) } 
  • 现在,从命令行中读取客户首选项并对患者列表进行排序。

     String sortType= sc.next() switch(sortType)){//Use java 7 or greater for string switch case "firsname": //Now sort the list by firstname using Comparator sort method. break; case "lastname": .... } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM