I'm trying to use scanner to load in an ArrayList of Modules, and then each module also contains an ArrayList of Students which are enrolled for that module. This is my constructor for the Modules:
public Module(String newCode, ArrayList<Student> students){ } The data is to be loaded from a text file which is laid out like this:
5 (number of modules) CS10110 (module code) 2 (number of students enrolled in the above module) fyb9 (student enrolled) lfr8 (student enrolled) CS10310 0 CS12130 1 fyb9 CS12230 1 lfr8 CS10610 2 fyb9 lfr8 I've been able to use scanner to load in students like so:
public void loadStudents(String fileName) throws FileNotFoundException{ Scanner infile =new Scanner(new InputStreamReader (new FileInputStream(fileName))); int num=infile.nextInt();infile.nextLine(); for (int i=0;i<num;i++) { String u=infile.nextLine(); String s=infile.nextLine(); String n=infile.nextLine(); String c=infile.nextLine(); Student st = new Student(u,s,n,c); students.add(st); } infile.close(); } But I am struggling with loading in an ArrayList with ArrayLists inside it, this is what my current crude code looks like :
public void loadModules(String fileName) throws FileNotFoundException{ Scanner infile =new Scanner(new InputStreamReader (new FileInputStream(fileName))); int num=infile.nextInt();infile.nextLine(); for (int i=0;i<num;i++){ String c=infile.nextLine(); ArrayList<Student> a =infile.next(); Module m = new Module(c,a); But obviously this does not work. Any ideas or tips here would be highly appreciated
ArrayList<Student> a = infile.next();this line will not compile.loadModulescode correctly.