How do I read a file into an array with Java using only the Scanner class? I'm not given the length of the file and am only allowed to use the Scanner class for this task, no more modern APIs or anything.
// load in file lines/content into array
public static String[] loadFile(String fname) {
Scanner fileIn = null;
try {
fileIn = new Scanner(new File(fname));
} catch (FileNotFoundException e) {
String[] emptyArr = new String[0];
return emptyArr;
}
int lineCount = 0;
while(fileIn.hasNextLine()) {
lineCount++;
fileIn.nextLine();
}
String[] fileContent = new String[lineCount];
fileIn.close();
return fileContent;
}``` This is all I have so far, not sure if this is down the right path
