简体   繁体   中英

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)

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 ();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM