0
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)

3
  • 1
    check how many tabulators the lines do have. They have to have at least 4 or you get your exception Commented Apr 7, 2016 at 6:11
  • To help you with this error, you have to post the file's content. I assume, that the format isn't the same in this row. There my be value[4] missing. Commented Apr 7, 2016 at 6:11
  • Check your input data and also debug in while loop. There should be some data which does have required index. Also you can do a length check on Array before printing. Commented Apr 7, 2016 at 6:11

1 Answer 1

2

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 ();
Sign up to request clarification or add additional context in comments.

10 Comments

my input in file is:
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
@PrasannModi That is different to what you posted above. Anyway try using a loop as in my answer. Could also be caused by a line with blanks on it
my input is: 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 and 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
Looks good then. If you are still having an error, then maybe due to a blank line
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.