【问题标题】:Separate a comma separated value in an array list then put it back together在数组列表中分隔逗号分隔值,然后将其重新组合在一起
【发布时间】:2015-03-20 17:11:35
【问题描述】:

我真正想做的是创建一个排序程序,根据用户在命令行中指定的任何内容对 PatientRecords 进行排序。

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

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

行数未指定,可能会有所不同,因此我使用的是数组列表。

我可以阅读这些行,我可以按姓氏对其进行排序,但在我看来,唯一的方法是用逗号分隔行并在单独的方法中单独理解它们.

如果有更好的方法请告诉我,我愿意接受任何事情。我的主要问题是让程序按不同的类别进行排序,例如 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()
      {

      }
 }

【问题讨论】:

  • 您在这里提出了许多问题。如果您放弃您的Object 恐惧症,它们都可以很容易地得到解答。将文件读入对象,对这些对象进行排序,然后写入这些对象。
  • 简单回答是的!你正在做的事情维护起来会非常乏味。您应该创建一个类并将数据填充到其中。然后创建该类的列表并编写自定义比较器进行排序。然后在该类中实现 toString 方法,以便您可以按照自己喜欢的方式将 hem 转换回字符串格式。

标签: java arrays sorting arraylist separator


【解决方案1】:
  • 创建一个名为 Patient 的模型类,其中包含名字、姓氏等。

    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":
      ....
    }
    

【讨论】:

  • 在这种情况下,switch.equals + else if 更具可读性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
相关资源
最近更新 更多