I have a text file with n amount of athletes names, each name on a new line, and the number of athletes is written as an int on the first line of the text file. The code i have written so far reads the text file, and using scanner and creates an array with all the data (Athletes names). What I wondering is, is it possible to use a method to create a separate array for each athlete (I will later fill these with data on scores/ performance). I cannot initialize these before the text file is scanned as the number of athletes will not yet be known.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TextScanner {
private static void readFile(String fileName) {
try {
List<String> distance = new ArrayList<String>();
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
distance.add(scanner.next());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
readFile(args[0]);
}
}
Very new to Java, so I apologise. I have looked around and cant seem to find any information. Thanks
Everytime the way which solves your problem is not the correct and only way!