问题描述
我是 Java 新手,需要一些帮助
I'm a java newbie and I need a some help
所以这是我的主要方法:
so here is my main method:
RegistrationMethods dmv = new RegistrationMethods();
ArrayList<CarOwner> ItState = new ArrayList<CarOwner>();
dmv.processTextToArrayList(ItState);
我有一个名为 CarOwner
的类,它有用于 firstName、lastName、license、month、year
实例变量的 getter 和 setter.
and I have a class called CarOwner
and it has getters and setters for firstName, lastName, license, month, year
instance variables.
这是我的 processTextToArrayList
方法头:
public void processTextToArrayList(ArrayList<CarOwner> inList) throws IOException
这个方法应该向传入的inList CarOwner
集合添加新的CarOwner
对象.对于csv文件的每一行,一个CarOwner
> 对象被添加到 inList
.
this method is supposed to add new CarOwner
objects to the inList CarOwner
collection passed in. For each line of the csv file, a CarOwner
object is added to inList
.
我必须从 csv 文件读取到 arraylist我的 csv 文件包含如下内容:
I have to read from csv file into arraylistmy csv file contains something like:
Bunny Bugs ACB-123 5 2013
Bunny Honey DEF-456 9 2013
Bunny Lola GHI-789 3 2014
我如何使用 while 循环对此进行编码?
how do I code this using while loop?
我的 CarOwner 类是:
my CarOwner class is :
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;
}
推荐答案
您可以使用这样的方法并提供您要读取的文件的路径.这将创建一个扫描器来读取传入的文件.
You could use a method like this and provide the path to the file you wish to read from.This creates a Scanner to read from the file passed in.
它一次抓取每一行并将一个新的 CarOwner(String,String,String,String,String) 对象添加到结果数组中.
It grabs each line one at a time and adds a new CarOwner(String,String,String,String,String) object to the result array.
附言我不知道你对 CarOwner 的实现,所以我只使用了所有的字符串......我会把它留给你来弄清楚.
P.S. i have no idea your implementation of CarOwner so i just used all Strings... I'll leave that to you to figure out heh.
public ArrayList < CarOwner > processTextToCarOwnerList(String filePath) throws IOException {
ArrayList < CarOwner > result = new ArrayList < CarOwner > ();
Scanner scan = new Scanner(new File(filePath));
while (scan.hasNextLine()) {
String line = scan.nextLine();
String[] lineArray = line.split(" ");
result.add(new CarOwner(lineArray[0], lineArray[1], lineArray[2], lineArray[3], lineArray[4]));
}
return result;
}
这篇关于java从csv文件中读取并将其信息存储到ArrayList<class>中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!