reading a .csv file into an array in java -
i have class called empquery trying create array of objects employee class hold data comes employeedatabase.csv file. database appears shown below. need use stream-processing-algorithm..
loop till eof{ read in 1 record deal record completly }
ex.
employee id,full name,department,start date,earnings emp001,adele m. fulton,engineering,10/28/2008,"$104,000.00" emp002,ali t. herman,engineering,2/27/2012,"$337,522.00" emp003,alika c. crawford,engineering,6/2/2009,"$144,000.00"
so far have set public class employeedb {
private static string[] empid = new string[300]; private static string[] empname = new string[300]; private static string[] department = new string[300]; private static string[] startdate = new string[300]; private static string [] earnings = new string[300]; private static string [] empdatabase = new string[300]; /** * @param args command line arguments * @throws java.io.filenotfoundexception */ public static void main(string[] args) throws filenotfoundexception { filldataarray(); } public class employee{ string empid; string empname; string department; string startdate; int earnings; public employee(string id,string name,string dept,string sdate,int earn){ empname = name; empid = id; department = dept; startdate = sdate; earnings = earn; } public employee( string id, string name) { empname = name; empid = id; department = ""; startdate = ""; earnings = 0; } public employee(){ empname = ""; empid = ""; department = ""; startdate = ""; earnings = 0; } } private static string[] filldataarray() throws filenotfoundexception { file databasefile = new file("empdb_lab7.csv"); scanner inputfile = new scanner(databasefile); string inputline; string [] empdbtemp = null; int i=0; while (inputfile.hasnextline()) { inputline = inputfile.nextline(); empdbtemp = inputline.split("-"); empid[i] = empdbtemp[1]; empname[i] = empdbtemp[2]; department[i] = empdbtemp[3]; startdate[i] = empdbtemp[4]; earnings[i] = empdbtemp[5]; } return empdbtemp; } } exception in thread "main" java.lang.arrayindexoutofboundsexception: 1 @ employeedb.employeedb.filldataarray(employeedb.java:76) @ employeedb.employeedb.main(employeedb.java:28) java result: 1
csv parsing harder appears, should use pre-existing library, such super csv
your csv column names should match bean field names (or skip header altogether), , earnings string, else you'll want use cellprocessor parse it.
reading stuff array , stream processing contradictory.
icsvbeanreader csvreader = new csvbeanreader(new filereader("employeedatabase.csv"), csvpreference.standard_preference); string[] header = csvreader.getheader(false); employeedb employeedb; while((employeedb = csvreader.read(employeedb.class, header)) != null) { system.out.println(employeedb.getempname()); } csvreader.close();
and
public class employeedb { private string empid; private string empname; private string department; private string startdate; private string earnings; ...getters/setters... }
i changed header to:
empid,empname,department,startdate,earnings
Comments
Post a Comment