-6
while ((line = inputStream.readLine()) != null) {

                    String[] words = line.split("\\s+");
                    String a=words[0];
                    String b=words[1];
                    String c=words[2];
}

while compiling I got error

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 " in "String b=words[1]".please provide some solution.

anju
  • 1
  • 2
  • Looks like your line wasn't split in 2 bits, so second part doesn't exist? Add an `if` to check `words`'s size, or learn more about your language. – J. Chomel Jun 08 '16 at 06:17

1 Answers1

0

Numbering for indices starts at 0 in Java. Your words array probably only contains 2 elements which is why you get the ArrayIndexOutOfBoundsException for the 3rd element.

Anyway it would be better, if you could actually provide the input string.

Try:

while ((line = inputStream.readLine()) != null) {

                    String[] words = line.split("\\s+");
                    if(words.length >= 3){
                        String a=words[0];
                        String b=words[1];
                        String c=words[2];
                    }                      
}
eol
  • 23,236
  • 5
  • 46
  • 64
  • After editing it is showing same error in "String b=words[1]". Actually i am trying to get the third element of a line. – anju Jun 08 '16 at 06:16
  • Well then your words array only contains one element. That's why you should provide the input string, so we can understand better what you're trying to achieve with your regex. – eol Jun 08 '16 at 06:18
  • Input- is of format 0.000000000 211.152.36.37 75.130.96.12 101 this is a single line of file . I am trying to read the file of this format containg 97178 such line and get the third element of each row. – anju Jun 08 '16 at 06:25
  • What is the third element? 75.130.96.12? – eol Jun 08 '16 at 06:39
  • yes....75.130.96.12 ok.....thanks ,now it is working. – anju Jun 08 '16 at 06:53
  • Nice! Accepting my answer would be appreciated :) – eol Jun 08 '16 at 07:15