I have a text file with the following content that I am trying to read into an ArrayList:
% There are 600 students. Each row is the set of desired courses (numbered from 1 to 18) for that student.
9. 13. 3. 7. 5. 8. 6. 12. 14. 11. 18. 15.
12. 1. 4. 16. 8. 5. 14. 3. 2. 6. 18. 9.
4. 16. 9. 13. 17. 6. 8. 14. 3. 11. 15. 10.
4. 16. 9. 13. 14. 11. 2. 15. 5. 10. 17. 8.
4. 16. 12. 1. 18. 14. 9. 17. 8. 5. 6. 11.
4. 16. 12. 1. 8. 5. 14. 11. 18. 10. 15. 2.
9. 13. 12. 1. 11. 10. 18. 8. 4. 2. 5. 15.
9. 13. 3. 7. 14. 11. 10. 4. 15. 18. 12. 17.
12. 1. 3. 7. 5. 10. 11. 6. 18. 14. 8. 9.
9. 13. 12. 1. 18. 10. 17. 3. 6. 14. 8. 15.
Below is my code, however it is not reading anything:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SchedReader {
public static void main(String[] args){
//reading
try{
File homedir = new File(System.getProperty("user.home"));
File fileToRead = new File(homedir, "workspace/Project1/sched.txt");
List<Integer> integers = new ArrayList<Integer>();
Scanner fileScanner = new Scanner(fileToRead);
fileScanner.nextLine();
fileScanner.useDelimiter(". ");
while (fileScanner.hasNextInt())
{
integers.add(fileScanner.nextInt());
}
System.out.println("Arraylist contains: " + integers.toString());
}
catch (Exception e){
System.out.println(e.toString());
}
}
}
Here is my output:
Arraylist contains: []
Could anybody please suggest how I can change my code to be able to load all the data into the ArrayList?
EDIT
So here's how I changed the code to advance through each line:
fileScanner.useDelimiter("[\\. \\n]+");
while (fileScanner.hasNextInt())
{
integers.add(fileScanner.nextInt());
fileScanner.hasNextLine();
fileScanner.nextLine();
}
However, it is still outputting to just one line.