java - Read and split text file into an array - Android -
i have gone through loads of these questions still cant seem figure out. have text file split rows. each row consists of 5 pieces of data separated ",". trying read file , split information string array in form:
string [][] xyz = new string [5][100];
please me out simple solution!? thanks!!! :)
data example:
john,22,1953,japan,green anna,18,2012,mexico,blue sam,34,1976,san francisco,pink
sample code:
public void readfile(){
assetmanager manger; string line = null; try { manger = getassets(); inputstream = manger.open("data.txt"); inputstreamreader isr = new inputstreamreader(is); bufferedreader br = new bufferedreader(isr); while ((line = br.readline()) != null) { values.add(line.split(",")); //system.out.print(value); } br.close(); string[] array = values.toarray(new string[20]; } catch (ioexception e1) { toast.maketext(getbasecontext(), "problem!", toast.length_short).show(); } }
errors
07-24 06:26:56.524: e/androidruntime(27203): fatal exception: main 07-24 06:26:56.524: e/androidruntime(27203): java.lang.arrayindexoutofboundsexception: length=10; index=10 07-24 06:26:56.524: e/androidruntime(27203): @ com.testingreadarray.weebly.testingreadarray.mainactivity.readfile(mainactivity.java:182) 07-24 06:26:56.524: e/androidruntime(27203): @ com.testingreadarray.weebly.testingreadarray.mainactivity$planonclicklistener.onitemselected(mainactivity.java:148) 07-24 06:26:56.524: e/androidruntime(27203): @ android.widget.adapterview.fireonselected(adapterview.java:895) 07-24 06:26:56.524: e/androidruntime(27203): @ android.widget.adapterview.access$200(adapterview.java:50) 07-24 06:26:56.524: e/androidruntime(27203): @ android.widget.adapterview$selectionnotifier.run(adapterview.java:863) 07-24 06:26:56.524: e/androidruntime(27203): @ android.os.handler.handlecallback(handler.java:615) 07-24 06:26:56.524: e/androidruntime(27203): @ android.os.handler.dispatchmessage(handler.java:92) 07-24 06:26:56.524: e/androidruntime(27203): @ android.os.looper.loop(looper.java:137) 07-24 06:26:56.524: e/androidruntime(27203): @ android.app.activitythread.main(activitythread.java:4867) 07-24 06:26:56.524: e/androidruntime(27203): @ java.lang.reflect.method.invokenative(native method) 07-24 06:26:56.524: e/androidruntime(27203): @ java.lang.reflect.method.invoke(method.java:511) 07-24 06:26:56.524: e/androidruntime(27203): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1007) 07-24 06:26:56.524: e/androidruntime(27203): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:774) 07-24 06:26:56.524: e/androidruntime(27203): @ dalvik.system.nativestart.main(native method)
you use bufferedreader read each line , split line using .split(',');
method.
some pseudo code:
bufferedreader bfr = new bufferedreader(inputstreamreader/filereader...); string line = null; int index = 0; string [][] xyz = new string [100][5]; while ( (line = bfr.readline()) != null) { if (index < xyz.length) xyz[index] = line.split(","); index++; }
hope helped!
Comments
Post a Comment