I have gone through loads of these questions but still cant seem to figure it out. I have a text file split into rows. Each row consists of 5 pieces of data separated by a ",". I am trying to read this file and split the information into a string array in this form:
String [][] xyz = new String [5][100];
Please could someone help me out with a 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 is = 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): at com.testingReadArray.weebly.testingReadArray.MainActivity.readFile(MainActivity.java:182) 07-24 06:26:56.524: E/AndroidRuntime(27203): at com.testingReadArray.weebly.testingReadArray.MainActivity$planOnClickListener.onItemSelected(MainActivity.java:148) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.widget.AdapterView.fireOnSelected(AdapterView.java:895) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.widget.AdapterView.access$200(AdapterView.java:50) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:863) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.os.Handler.handleCallback(Handler.java:615) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.os.Handler.dispatchMessage(Handler.java:92) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.os.Looper.loop(Looper.java:137) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.app.ActivityThread.main(ActivityThread.java:4867) 07-24 06:26:56.524: E/AndroidRuntime(27203): at java.lang.reflect.Method.invokeNative(Native Method) 07-24 06:26:56.524: E/AndroidRuntime(27203): at java.lang.reflect.Method.invoke(Method.java:511) 07-24 06:26:56.524: E/AndroidRuntime(27203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 07-24 06:26:56.524: E/AndroidRuntime(27203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 07-24 06:26:56.524: E/AndroidRuntime(27203): at dalvik.system.NativeStart.main(Native Method)