3

I have a tab delimited file and i have to read the data from the file.

Col1    Col2    Col3
data1   data2   data3
data1   data2   data3

if values are present for all columns then no isssues. the problem is sometimes the few columns may not contain values as below.

Col1    Col2    Col3
data1           data3
data1   data2   

in above data, am able to read first rows data as col2's value will be empty string. But the second row's col3 has no data. here i get array index out of bounds exception. why am i not getting empty string for the second row's col3?

i am using the code as below:

String dataFileName = "C:\\Documents and Settings\\User1\\some.txt";

         /**
          * Creating a buffered reader to read the file
          */
         BufferedReader bReader = new BufferedReader(
                 new FileReader(dataFileName));

         String line;

         /**
          * Looping the read block until all lines in the file are read.
          */
         while ((line = bReader.readLine()) != null) {

             /**
              * Splitting the content of tabbed separated line
              */
             String datavalue[] = line.split("\t");
             String value1 = datavalue[0];
             String value2 = datavalue[1];
             String value3 = datavalue[2];
}

Thanks!

4
  • 5
    Not an answer to your question, but some common guidance. Instead of reinventing the wheel you should consider using some library to read inn CSV files. Like CSVReader: javacsv.sourceforge.net/com/csvreader/CsvReader.html
    – philnate
    Apr 18, 2013 at 9:49
  • 1
    @philnate is correct, I was just writing the same comment. Most CSV libraries support arbitrary delimiters (e.g. tabs). I would recommend you switch sooner rather than later! It may help you solve this and other problems, such as quoted text etc. Apr 18, 2013 at 9:49
  • why are you not using a database for this tabular structure Apr 18, 2013 at 9:53
  • 2
    @Philnate I think this IS an answer to the question. Parsing CSV is one of those tasks that you do not want to reinvent. Parsing anything really.
    – carbontax
    Apr 18, 2013 at 9:55

2 Answers 2

3

The lazy way would be something like:

...
String datavalue[] = Arrays.copyOf(line.split("\t"),3);
String value1 = datavalue[0];
String value2 = datavalue[1];
String value3 = datavalue[2];
...

Basically you are splitting the content and copying it to a new array in which the padded elements are null as documented:

Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array.

0
2

String.split by default merges duplicate separators into one. You should use a negative second parameter:

String datavalue[] = (line+"\t\t\t").split("\t",-1);

Also - in case the original file is missing trailing tabs you can add extra tabs to the line to stop this breaking your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.