Deslyxia
Deslyxia

Reputation: 629

File IO Exceptions in Java

I am reading input from a tab delimited file in a java class. The file opens properly and the information from the file seems to be read in correctly as well. Every line in the file winds up printing to the screen as expected but then at the end of file it seems like it tries to print one more line and I get an ArrayIndexOutOfBoundsException: 1.

It is worth noting that if I uncomment the line where I output the value of sCurrentline and comment out the output of the split array I do not get the error.

Code:

BufferedReader br = null;

try {
        String sCurrentLine;

        br = new BufferedReader(new FileReader(fname));

        while ((sCurrentLine = br.readLine()) != null){

            String[] values = sCurrentLine.split("\\t", -1); // don't truncate empty fields

            System.out.println("Col1: " + values[0] + " Col2: " + values[1] + " Col3: " 
            + values[2] + " Col4: " + values[3] + " Col5: " + values[4] );

            //System.out.println(sCurrentLine);

        }
} catch (IOException e) {
    System.out.println("IOException");
    e.printStackTrace();
} finally {
    try {
        if(br != null){
            br.close();
        }
    } catch (IOException ex) {
        System.out.println("ErrorClosingFile");
        ex.printStackTrace();
    }
}

Upvotes: 0

Views: 77

Answers (3)

paul
paul

Reputation: 13481

Try this

    String[] values = "".split("\\t", -1); // don't truncate empty fields
    int index=1;
    StringBuffer sb = new StringBuffer();
    for (String value : values) {
         sb.append("Col"+index+":").append(value).append(" ");
        index++;
    }
    System.out.println(sb.toString());

Is obvious that you are reading an array position that does not exist

Upvotes: 0

stevecross
stevecross

Reputation: 5684

The last line has not the same amount of elements as the other lines. After splitting the last line, you try to access fields of the array, that do not exist. That is indicated by the exception http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html. Before you access the fields of the array, you have to check if there is the expected amount of items in it. Like this:

BufferedReader br = null;

try {
    String sCurrentLine;
    br = new BufferedReader(new FileReader(fname));

    while ((sCurrentLine = br.readLine()) != null){
        String[] values = sCurrentLine.split("\\t", -1); // don't truncate empty fields

        if (5 == values.length) {
            System.out.println("Col1: " + values[0] + " Col2: " + values[1] + " Col3: " 
            + values[2] + " Col4: " + values[3] + " Col5: " + values[4] );
        }

        // System.out.println(sCurrentLine);
    }
} catch (IOException e) {
    System.out.println("IOException");
    e.printStackTrace();
} finally {
    try {
        if(br != null){
            br.close();
        }
    } catch (IOException ex) {
        System.out.println("ErrorClosingFile");
        ex.printStackTrace();
    }
}

Upvotes: 2

Exceptyon
Exceptyon

Reputation: 1582

the code seems ok... do you have an empty newline at the end?

while ((sCurrentLine = br.readLine()) != null){
    if (sCurrentLine.isEmpty() || sCurrentLine.startsWith(";")) // skip empty and comment lines
        continue;

    String[] values = sCurrentLine.split("\\t"); // are you sure the -1 is required?
...
}

Upvotes: 1

Related Questions