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?