[英]Reading txt file and putting lines into List as object
假设我们有一个像这样的文本文件:
我想做的是使用DatePeople和DateComparator类(使用collection.sort)以降序生成列表。
我真正无法理解的是在阅读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:
public class DatePeople
{
DatePeople(){
}
private String name;
private int day;
private int month;
private int year;
}
DateComparator:
public class DateComparator implements Comparator<DatePeople> {
public DateComparator(){
}
@Override
public int compare(DatePeople o1, DatePeople o2) {
return 0;
}
}
您需要为每行创建一个对象,然后才能将其添加到对象列表中
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();
您还需要在DatePeople类中创建一个构造函数,该构造函数使用name,year,month,day参数。
就目前而言,您可以为年,月和日设置三个变量。 不知道这是否是故意的。 或者,您可以在班级中仅包含名称和日期变量。
如果您知道数据是标准化的,则可以根据已知规则对其进行解析。
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));
然后,您将需要一个构造函数,在其中传递DatePeople中的值,即:
DatePeople(String n, int y, int m, int d) {
this.name = n;
this.year = y;
this.month = m;
this.day = d;
}
另外,在DatePeople中可以有一个parseDatePerson(String line){}方法,其中包含我的第一个代码段,然后您只需将
list.add(new DatePeople(s.nextLine()));
这将在DatePeople中调用如下所示的构造函数:
DatePeople(String line) {
parseDatePerson(line);
}
日期比较器
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;
}
}
}
}
}
约会人
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;
}
}
主要
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.