6

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

and I have a class called CarOwner and it has getters and setters for firstName, lastName, license, month, year instance variables.

And this is my method header for processTextToArrayList method:

public void processTextToArrayList(ArrayList<CarOwner> inList) throws IOException

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.

I have to read from csv file into arraylist my csv file contains something like:

Bunny Bugs ACB-123 5 2013

Bunny Honey DEF-456 9 2013

Bunny Lola GHI-789 3 2014

how do I code this using while loop?

edit:

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

}

and my Citizen class is also:

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;
}
1
  • 1
    Please make an attempt to show us the CarOwner class. And please expand processTextToArrayList. It needs to have something in there which reads from a file. Take a look at stackoverflow.com/questions/224952/…. Commented Dec 5, 2014 at 20:46

2 Answers 2

6

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.

It grabs each line one at a time and adds a new CarOwner(String,String,String,String,String) object to the result array.

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You could use something like this:

ArrayList<CarOwner> owners = new ArrayList<CarOwner>();

BufferedReader br = new BufferedReader(new FileReader(new File("path/to/your/file.csv")));
String line;
while ((line = br.readLine()) != null) {

    String[] entries = line.split(",");

    CarOwner owner = new CarOwner(entires[0], entries[1], entries[2], entries[3]);

    owners.add(owner);
}

Do you have a real csv file (all values separated by ,) or are they separated by spaces or something like that? In that case you'd have to replace the , with spaces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.