tokenizing tab delimited text into 2D array

I'm trying to tokenize a tab delimited file into a 2 dimensional array, and I'm using the split method.

Each row of the file will insert its data into a new array row.

The array is statically defined as follows:

String[][] arr = new String [50][37];

Which will allow a total of 50 rows of data to be placed into the array.

I was originally using StringTokenizer to parse the file, until I realized that it doesn't return empty strings, which I need, because the first array row and any other array row need to have correspondence (e.g. arr[0][3] is a question string, arr[1][3] is the answer for that question in one row, arr[4][3] is the answer for that question in another row).

The original array returned from the split should have 36 elements, which should mean that element 37 (index 36) is null.However I want to statically define arr[0][36] to be a specific string, as it will always be used and will not change value.

Now my problem is that even though I have statically defined the array column to be 37 elements big, when printing out the array using a for loop, I get an ArrayIndexOutOfBoundsException when array index 36 is called. To test my problem, I did this:

String[] tempStr = new String[37];

System.out.println("tempStr length1: " + tempStr.length);

line = bfread.readLine();

tempStr = line.split("\u0009"); //tab char

System.out.println("tempStr length2: " + tempStr.length);

which outputs:

tempStr length1: 37

tempStr length2: 36

Can anyone help me out, or does anyone know why this is happening? I'd really appreciate some help -- I'm a Java newb, and this is really getting me frustrated.

Thanks!

[1741 byte] By [eurowerke] at [2007-9-30 14:40:02]
# 1

Per the String.class API document, split(String regex) returns a String array. It can split a string into more than 2 pieces if it matches more than one place in the string. I assume that you only will get one match, resulting in 2 elements in the array.

tempStr refers to the array; the elements are tempStr[0] and (maybe) tempStr[1]. I say maybe, because if the split regex doesn't match, then referring to tempStr[1] will cause a runtime error,

java.lang.ArrayIndexOutOfBoundsException.

The length of the array is the number of array elements, not the length of the element strings. If you want the length of the strings, use something like tempStr[0].length{}.

Run this example code

String s = "qwerty7uiop";

String[] a = s.split("\u0007");// this is the "7"

System.out.println(a[0] + " " + a[1]);

System.out.println(a.length); // 2 elements, a[0] and a[1]

ChuckBing at 2007-7-5 14:19:34 > top of Java-index,Administration Tools,Sun Connection...