【发布时间】:2015-02-04 02:42:42
【问题描述】:
我是 Java 新手,我需要一些帮助
所以这是我的主要方法:
RegistrationMethods dmv = new RegistrationMethods();
ArrayList<CarOwner> ItState = new ArrayList<CarOwner>();
dmv.processTextToArrayList(ItState);
我有一个名为CarOwner 的类,它有firstName, lastName, license, month, year 实例变量的getter 和setter。
这是我processTextToArrayList 方法的方法头:
public void processTextToArrayList(ArrayList<CarOwner> inList) throws IOException
此方法应该将新的CarOwner 对象添加到传入的inList CarOwner 集合中。对于csv 文件的每一行,将CarOwner 对象添加到inList。
我必须从 csv 文件读取到 arraylist 我的 csv 文件包含以下内容:
Bunny Bugs ACB-123 5 2013
Bunny Honey DEF-456 9 2013
Bunny Lola GHI-789 3 2014
如何使用 while 循环进行编码?
编辑:
我的 CarOwner 课程是:
public class CarOwner extends Citizen implements CarOwnerInterface, Serializable
{
private String license;
private int month, year;
public CarOwner()
{
super();
license = "Not Assigned";
month = 0;
year = 0;
}
public CarOwner(String inFirst, String inLast, String inLicense, int inMonth, int inYear)
{
super(inFirst, inLast);
license = inLicense;
month = inMonth;
year = inYear;
}
public void setLicense(String inLicense)
{
license = inLicense;
}
public String getLicense()
{
return license;
}
public void setMonth(int inMonth)
{
month = inMonth;
}
public int getMonth()
{
return month;
}
public void setYear(int inYear)
{
year = inYear;
}
public int getYear()
{
return year;
}
public int compareTo(Object o)
{
if ((o != null ) && (o instanceof CarOwner))
{
CarOwner otherOwner = (CarOwner) o;
if (otherOwner.compareTo(getYear()) > 0)
return -1;
else if (otherOwner.compareTo(getYear()) < 0)
return 1;
else if (otherOwner.equals(getYear()))
if (otherOwner.compareTo(getMonth()) > 0)
return -1;
else if (otherOwner.compareTo(getMonth()) < 0)
return 1;
else if (otherOwner.equals(getMonth()))
return 0;
}
return -1;
}
}
我的公民课也是:
public class Citizen implements CitizenInterface, Serializable
{
private String firstName, lastName;
public Citizen()
{
firstName = "No Name";
lastName = "No Name";
}
public Citizen(String inFirstName, String inLastName)
{
firstName = inFirstName;
lastName = inLastName;
}
public void setFirstName(String inFirst)
{
firstName = inFirst;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String inLast)
{
lastName = inLast;
}
public String getLastName()
{
return lastName;
}
public String toString()
{
String str;
str = firstName + " " + lastName;
return str;
}
【问题讨论】:
-
请尝试向我们展示 CarOwner 类。请展开 processTextToArrayList。它需要有一些从文件中读取的东西。看看stackoverflow.com/questions/224952/…。