我是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(String,String,String,String,String)对象。

P.S.我不知道您对CarOwner的实现,所以我只使用了所有Strings ...我会留给您找出原因。

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;
    }

09-11 20:05