java reading from csv file and storing its information into ArrayList<class> -
i'm java newbie , need help
so here main method:
registrationmethods dmv = new registrationmethods(); arraylist<carowner> itstate = new arraylist<carowner>(); dmv.processtexttoarraylist(itstate);
and have class called carowner
, has getters , setters firstname, lastname, license, month, year
instance variables.
and method header processtexttoarraylist
method:
public void processtexttoarraylist(arraylist<carowner> inlist) throws ioexception
this method supposed add new carowner
objects inlist carowner
collection passed in. each line of csv file, carowner
object added inlist
.
i have read csv file arraylist csv file contains like:
bunny bugs acb-123 5 2013 bunny honey def-456 9 2013 bunny lola ghi-789 3 2014
how code using while loop?
edit:
my carowner class :
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 citizen class 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; }
you use method , provide path file wish read from. creates scanner read file passed in.
it grabs each line 1 @ time , adds new carowner(string,string,string,string,string) object result array.
p.s. have no idea implementation of carowner used strings... i'll leave 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; }
Comments
Post a Comment