简体   繁体   English

读取txt文件并将行作为对象放入列表

[英]Reading txt file and putting lines into List as object

Lets say that we have a text file like this: 假设我们有一个像这样的文本文件:

what i am trying to do is produce a list in descending order with using a DatePeople and DateComparator classes (using collection.sort) 我想做的是使用DatePeople和DateComparator类(使用collection.sort)以降序生成列表。

what i couldnt really understand is after reading txt file how do i put them in correct way into arraylist as DatePeople object ?? 我真正无法理解的是在阅读txt文件后,如何将它们以正确的方式作为DatePeople对象放入arraylist中?

List<DatePeople> list = new ArrayList();
        Scanner filenamereader = new Scanner(System.in);
        System.out.println("Enter file name(input.txt): ");
        String fileName = filenamereader.next();
        System.out.println(fileName);
        filenamereader.close();
        try{
            Scanner s = new Scanner(new File(fileName));

            while (s.hasNext()){
                list.add()); ??
        //list.add(new DatePeople(name,year,month,day)); something like this i guess ?
            }
            s.close();

        }catch(IOException io){

            io.printStackTrace();
        }

DatePeople: DatePeople:

public class DatePeople
{

    DatePeople(){

    }
        private String name;
        private int day;
        private int month;
        private int year;


    }

DateComparator: DateComparator:

public class DateComparator implements Comparator<DatePeople> {
public DateComparator(){

    }

    @Override
    public int compare(DatePeople o1, DatePeople o2) {




        return 0;
    }
}

You will need to create an object for each line before you can add it to the list of objects 您需要为每行创建一个对象,然后才能将其添加到对象列表中

 while (s.hasNext()){
//create your DatePeople object here
// then add it to the list
DatePeople obj = new DatePeople(name,year,month,day); 
                list.add(obj); 
        //list.add(new DatePeople(name,year,month,day)); something like this i guess ?
            }
            s.close();

You also need to create a constructor in your DatePeople class that takes the name,year,month,day arguments. 您还需要在DatePeople类中创建一个构造函数,该构造函数使用name,year,month,day参数。

As it stands you have three variable for your year,month and day. 就目前而言,您可以为年,月和日设置三个变量。 Not sure if that is intentional. 不知道这是否是故意的。 Or you could just have name and date variables in your class. 或者,您可以在班级中仅包含名称和日期变量。

If you know that the data is standardized, then you can parse it based on the known rules. 如果您知道数据是标准化的,则可以根据已知规则对其进行解析。

String line = s.nextLine();
String[] bits = line.split(" ", 2);
String name = bits[0];
String[] dateBits = bits[1].split("-", 3);
int year = Integer.parseInt(dateBits[0]);
int month = Integer.parseInt(dateBits[1]);
int day = Integer.parseInt(dateBits[2]);

list.add(new DatePeople(name, year, month, day));

Then you'll need a constructor where you pass values in DatePeople, ie: 然后,您将需要一个构造函数,在其中传递DatePeople中的值,即:

DatePeople(String n, int y, int m, int d) {
    this.name = n;
    this.year = y;
    this.month = m;
    this.day = d;
}

Alternatively, you could have a parseDatePerson(String line) {} method in DatePeople that contains my first code segment, and then you simply put 另外,在DatePeople中可以有一个parseDatePerson(String line){}方法,其中包含我的第一个代码段,然后您只需将

list.add(new DatePeople(s.nextLine()));

This would call a constructor in DatePeople that looks like: 这将在DatePeople中调用如下所示的构造函数:

DatePeople(String line) {
    parseDatePerson(line);
}

DateComparator 日期比较器

import java.util.*;
import java.io.File;
class DateComparator implements Comparator<DatePeople> {
    @Override
    public int compare(DatePeople o1, DatePeople o2) {
        if(o1.year>o2.year)
        {
          return -1;
        }
        else if(o1.year<o2.year){
          return 1;
        }
        else
        {
          if(o1.month>o2.month)
          {
            return -1;
          }
          else if(o1.month<o2.month){
            return 1;
          }
          else{
            if(o1.day>o2.day)
            {
              return -1;
            }
            else if(o1.day<o2.day){
              return 1;
            }
            else{
              return 0;
            }
          }
        }
    }
}

DatePeople 约会人

class DatePeople
{
         String name;
         int day;
         int month;
         int year;

    DatePeople(String name,int year,int month,int day){
      this.name=name;
      this.day=day;
      this.month=month;
      this.year=year;
    }

    }

Main 主要

class Main {
  public static void main(String[] args) {
    List<DatePeople> list = new ArrayList();
        Scanner filenamereader = new Scanner(System.in);
        System.out.println("Enter file name(input.txt): ");
        String fileName = filenamereader.next();
        System.out.println(fileName);
        filenamereader.close();
        try{
            Scanner s = new Scanner(new File(fileName));
            while (s.hasNext()){
              String s1[]=s.nextLine().split(" ");
              String s2[]=s1[1].split("-");
              list.add(new DatePeople(s1[0],Integer.parseInt(s2[0]),Integer.parseInt(s2[1]),Integer.parseInt(s2[2])));
            }
            Collections.sort(list,new DateComparator());  
            for(DatePeople dp:list)
            {
              System.out.println(dp.year+"-"+dp.month+"-"+dp.day+" "+dp.name);
            }

            s.close();

        }catch(Exception io){

            io.printStackTrace();
        }
  }
}

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

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