I am writing a program where I have to transfer all the data of a file to an array, line by line. When I display the line though, the only thing in the array is the last line. I need the array to have all the lines of the file so I can select an index of the array.
Here is the code so far,
while(inputFileTest.hasNext()) //counts amount of lines in the file
{
count++;
inputFileTest.nextLine();
}
fileTest= new File("TestBank.txt");
inputFileTest= new Scanner(fileTest);
String[] testArr=new String[count];
while(inputFileTest.hasNext())
{
int i=0;
String line= inputFileTest.nextLine();
testArr= line.split("\n");
testArr[i]=testArr[0];
i++;
}
//int i=rand.nextInt(testArr.length);
for(String test:testArr)
System.out.println(test);
} }