-3

I have installed Gradle and i am trying to read some data from a file.

The file is

   A; 1;2
    B;2;3
    C;4;5
    H;5;6
.
.
.

and i am trying to do something special on the first 5 records

InputStream inputFileStream = Main.class.getResourceAsStream("/inputFile.txt");
Scanner scanner = new Scanner(inputStream);
        int counter = 5;

         while (scanner.hasNext()) {
                String line = scanner.nextLine();

                String[] array;
                if(counter>=0)   
                {

                    array = line.split(";");
                    System.out.println(line);

            //      System.out.println("Hero is:  "+array[0]+"  "+array[1]+"  "+array[2]);       
                     counter--;
                }   

         }

The problem is that, in line

System.out.println("Hero is:  "+array[0]+"  "+array[1]+"  "+array[2]);

i face with an error:

java.lang.ArrayIndexOutOfBoundsException: 1

I know, that it means i am asking to get a value outside of the array, but i don't know why it is happening? and how can i fix it?

For downvoters: I am using System.out.println(array-length);

before and after erroring line, but the strange thing is that when i have the erroring line nothing is printed?

Jeff
  • 7,767
  • 28
  • 85
  • 138
  • 2
    Perhaps the split didn't give more than a single return. Try printing `array.length` to see how many elements it actually has. – KevinO Apr 03 '16 at 21:21
  • that's a too optimistic split, what do you think happen for the "." lines? –  Apr 03 '16 at 21:22
  • 2
    Also, really, learn to use a debugger, or at least to add System.out.println lines in the code, to see what your variables contain and where the problem comes from. You shouldn't need us to diagnose that kind of problem. – JB Nizet Apr 03 '16 at 21:24
  • 1
    @RC. That just means that OP cutted the file for this post .. just a simple "...". (at least it would make no sense to have "." in that file :D) – Tom Apr 03 '16 at 21:24
  • @JBNizet, there was the old saying from Kernighan: "The most effective debugging tool is still careful thought, coupled with judiciously placed print statements". – KevinO Apr 03 '16 at 21:26
  • I have update my question – Jeff Apr 03 '16 at 21:27
  • 2
    *when i have the erroring line nothing is printed?*: that is a strong indication that the line is an empty string. You have a blank line in your file. – JB Nizet Apr 03 '16 at 21:28
  • It appears I cannot post an answer. However, add a check `if (array.length > 2)` before the `System.out.println("Hero "...)` line. It will prevent the issue. As others noted, undoubtedly the problem is a blank or malformed line in the input file. – KevinO Apr 03 '16 at 21:44

1 Answers1

0

At the first iteration, the array size is only 1, so array[1] throws ArrayIndexOutOfBoundsException

You need to call System.out.println("Hero is: "+array[0]+" "+array[1]+" "+array[2]); outside of the loop.

InputStream inputFileStream = Main.class.getResourceAsStream("/inputFile.txt");
Scanner scanner = new Scanner(inputStream);
    int counter = 5;
         while (scanner.hasNext()) {
               String line = scanner.nextLine();
               String[] array;
               if(counter>=0)   
               {
                   array = line.split(";");
                   System.out.println(line);
                   counter--;
               }
         }
System.out.println("Hero is:  "+array[0]+"  "+array[1]+"  "+array[2]);       
Quan Do
  • 141
  • 8