我是一個Java新手,我需要一些幫助的java從csv文件讀取和存儲其信息到ArrayList中<class>
所以這裏是我的主要方法:
RegistrationMethods dmv = new RegistrationMethods();
ArrayList<CarOwner> ItState = new ArrayList<CarOwner>();
dmv.processTextToArrayList(ItState);
和我有一個類稱爲CarOwner
並且它具有用於firstName, lastName, license, month, year
實例變量的獲取器和設置器。
這是我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。它需要有一些內容從文件讀取。看看http://stackoverflow.com/questions/224952/most-concise-way-to-read-the-contents-of-a-file-input-stream-in-java。 – rajah9 2014-12-05 20:46:42