Android app csv file getting java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 -
i have csv file inside raw folder has multi columns, i'm able read first column seems produce of data in csv file, not want, when trying read next :
java.lang.arrayindexoutofboundsexception: length=1; index=1
i tried different ways of writing code below , keep getting same result, here snippet of csv file:
organisationid organisationcode 1421 rv9he
can please advise me i'am going wrong , why keep getting, please?
caused by:java.lang.arrayindexoutofboundsexception: length=1; index=1 ?
i'm pulling hair out this, csv file has no columns , i'm reading header of csv file ?
private string organisationid; private string organisationcode; public string getorganisationid() { return organisationid; } public void setorganisationid(string organisationid) { organisationid = organisationid; } public string getorganisationcode() { return organisationcode; } public void setorganisationcode(string organisationcode) { organisationcode = organisationcode; } while ((line = reader.readline()) != null) { string[] rowdata = line.split(","); string setid = rowdata[0]; string setcode = rowdata[1]; hospitalreader cur = new hospitalreader(); cur.setorganisationid(setid); cur.setorganisationcode(setcode); this.add(cur); }
i don't see wrong code giving example hoping able spot if there issue input.
input content (xyz.csv):
"organisationid","organisationcode" "1421","rv9he"
code: taken https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/
string csvfile = "xyz.csv"; bufferedreader br = null; string line = ""; string cvssplitby = ","; try { br = new bufferedreader(new filereader(csvfile)); while ((line = br.readline()) != null) { // use comma separator string[] txt = line.split(cvssplitby); system.out.println(txt[0] + "....." + txt[1]); } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { if (br != null) { try { br.close(); } catch (ioexception e) { e.printstacktrace(); } } }
output:
"organisationid"....."organisationcode" "1421"....."rv9he"
Comments
Post a Comment