android - Pulling values from an CSV file in java -


i having trouble pulling values csv file android app working on. csv file takes following format:

acton town (district),acton town (piccadilly),2.00 aldgate (circle),aldgate (metropolitan),4.00 aldgate east (district),aldgate east (hammersmith , city),2.00 

i trying import java class using following method:

public arraylist<connection> importconnections(){         try          {             //gets lines.txt file assets             in = this.getassets().open("connections.csv");             scanner scan = new scanner(in);             textview linedata = (textview)findviewbyid(r.id.displayline);             string connection = null;             string startingstation = null;             string endingstation = null;             float duration = 0;                         {                 connection = scan.nextline();                 string delimiter = ",\n";                 stringtokenizer tokens = new stringtokenizer(connection, delimiter);                 startingstation = tokens.nexttoken();                 endingstation = tokens.nexttoken();                 duration = float.parsefloat(tokens.nexttoken());                 connections.add(startstation, endstation, duration);             }             while(scan.hasnext());             //for testing purposes             linedata.settext(endingstation);         }          catch (ioexception e)          {             // todo auto-generated catch block             e.printstacktrace();         }         return connections;     } 

if run this, app crash , can't figure out why. if set linedata.settext(endingstation) linedata.settext(connection), display entire row e.g:

acton town (district),acton town (piccadilly),2.00 

if comment out

duration = float.parsefloat(tokens.nexttoken());

it run method. can find fault method?

you try using opencsv http://opencsv.sourceforge.net/

it pretty simple use , returns array of each row.

csvreader reader = new csvreader(new filereader(<filepath>));  string[] temp; int num = #; //number of rows in csv, or insert code read through file until null.  for(int = 0; < num; i++) {                    temp = reader.readnext();               //read next line temp }  system.out.println(temp[0]); //acton town (district) system.out.println(temp[1]); //acton town (piccadilly) system.out.println(temp[2]); //2.00 (string)   reader.close(); 

like said, easy use , prevents having parse out string on own.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -