0

I am having trouble attempting to store each line in a text file into an array. So far, my program is only able to read the file. Here is what i have so far.

public class assignment1 {

    public static void main(String[] args) {
        System.out.println(System.getProperty("user.dir"));
        File fileName = new File("competitors.txt");
        try {
            Scanner scnr = new Scanner(fileName);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
D.Nik
  • 13
  • 4

1 Answers1

0

You could loop over the Scanner until it has no more lines:

List<String> list = new LinkedList<>();
try (Scanner scanner =  new Scanner(fileName)) {
    while (scanner.hasNextLine()) {
        list.add(scanner.nextLine());
    }
} catch (FileNotFoundException e) {
    System.out.println(e);
}
String[] array = list.toArray(new String[list.size()]);
Mureinik
  • 297,002
  • 52
  • 306
  • 350