Prasann Modi
Prasann Modi

Reputation: 15

while reading .txt file in java getting ArrayIndexOutOfBoundsException

try (BufferedReader br = new BufferedReader(new FileReader("/home/modi/Desktop/hbaseoff/mobiledata.txt")))
        {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                String[] values = sCurrentLine.split("\t");
System.out.println(values[0]+"-"+values[1]+"\t"+values[2]+"\t"+values[3]+"\t"+values[4]);
}

        } catch (IOException e) {
            e.printStackTrace();
        } 

output:

20150320-9876543217 16  45  22
20150320-8876543218 45  11  13
20150320-8876543219 49  15  16
20150321-9876543210 16  45  22
20150321-9876543211 45  11  13

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Jclass.main(Jclass.java:18)

Upvotes: 0

Views: 239

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44813

Because there are only 4 tab delimitered values

try

System.out.println(values[0]+"-"+values[1]+"\t"+values[2]+"\t"+values[3]);

Personally I would put this in a loop

for (String val : values) {
   System.out.print (val + "\t");
}
System.out.println ();

Upvotes: 2

Related Questions