0

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)

Alex
  • 15
  • 1
  • 2
  • 7

4 Answers4

3

You could use a BufferedReader to read each line and then split the line using the .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 this helped!

wdziemia
  • 1,429
  • 1
  • 14
  • 19
  • IT says "Type mismatch: cannot convert from String[] to String" wrt "xyz[index] = line.split(",");" – Alex Jul 24 '13 at 04:11
  • Make sure xyz[index] is a multidimensional array when you declare it. If its not xyz[index] will return a String, not a String-array. – wdziemia Jul 24 '13 at 04:15
  • Yeh It is... and ive reduced everything to just read the first 10 values... my app just crashes once I call for it to read and then display the values. :/ – Alex Jul 24 '13 at 04:19
  • in your catch block, put e1.printStackTrace(); Go into logcat and paste your error in the main post. – wdziemia Jul 24 '13 at 04:22
  • Hope that helps? Quite new to making apps :) – Alex Jul 24 '13 at 04:32
  • java.lang.ArrayIndexOutOfBoundsException: length=10; index=10 This is your problem. Your array is too small. See my answer for fix – wdziemia Jul 24 '13 at 04:37
  • AH so happy now... added in the "if (index < xyz.length) xyz[index] = line.split(",");" and its working! Thanks so much! :D – Alex Jul 24 '13 at 04:59
1

I assume you understand the basics of reading through a file. So let's start at where you have your String s equal to one full row:

String s = "John,22,1953,Japan,Green";

You can call the split() function to return an array from s subdivided by some expression (in this case, a comma) like so:

String[] row = s.split(","); //Returns a new String array {John,22,1953,Japan,Green}

Then you have row[0] = "John", row[1] = 22, and so on. You can do whatever you would like with these arrays, including store them in a multidimensional array if that's what you want to do.

String[][] xyz = new String[numRows][5];
xyz[0] = row;
System.out.println(xyz[0][0]); //prints "John"

Hopefully that's clear and I understood what you were trying to do correctly.

Kon
  • 10,702
  • 6
  • 41
  • 58
  • Yeah I understand how that works... but going from reading it from a file to getting it into the array is where it messes me about. Once I can just get the data into an array I'll be able to manipulate it... but getting it from TXT file into Array is killing me! Lol. :) – Alex Jul 24 '13 at 04:13
1

See Using BufferedReader.readLine() in a while loop properly for an example of reading a file line by line using a buffered reader, then combine this with Kon's answer and I think you have a solution.

AssetManager manger;
String line = null;
List<String[]> xyzList = new ArrayList<String[]>();
String[][] xyz;  
InputStream is;
InputStreamReader isr;
BufferedReader br;
try {
    manger = getAssets();
    is = manager.open("data.txt");
    isr = new InputStreamReader(is);
    br = new BufferedReader(isr);
    while ((line = br.readLine()) != null) {
      xyzList.add(line.split(","));
    }
    xyz = (String[][])xyzList.toArray();
}catch (IOException e1) {
    Toast.makeText(getBaseContext(), "Problem!", Toast.LENGTH_SHORT).show();
}finally{
    br.close();
    isr.close();
    is.close();
}
Community
  • 1
  • 1
Jeremy Farrell
  • 1,481
  • 14
  • 16
0
/* Assuming you want a 5 by n matrix from this data
John,22,1953,Japan,Green
Anna,18,2012,Mexico,Blue
Sam,34,1976,San Francisco,Pink
.
.
.
.
nth row

String[] row = s.split(","); */
for (int i = 0; i<n i++)
{
    for(int j = 0; j<originalString.length; j++)
    {
        xyz[i] = originalString[j].split(",");
    }
}