0

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

4
  • Are you sure you are copy-pasting the same code? cause ArrayList<Student> a = infile.next(); this line will not compile. Commented Feb 2, 2014 at 19:44
  • Yeah that's the problem it doesn't work. This was my attempt at getting it working, but clearly it's not working. Commented Feb 2, 2014 at 19:49
  • Please copy-paste the loadModules code correctly. Commented Feb 2, 2014 at 19:50
  • That is all I have for loadModules and I don't how how it should look Commented Feb 2, 2014 at 19:54

2 Answers 2

1

infile.next() returns a String value, so it cannot populate an ArrayList<Student>. A general way of populating an ArrayList<ArrayList<String>> from a text file might be as follows:

public ArrayList<ArrayList<String>> readFile(String filename) throws IOException { FileInputStream fis = new FileInputStream(filename); Scanner sc = new Scanner(fis); ArrayList<ArrayList<String>> modulesList = new ArrayList<>(); int numModules = sc.nextInt(); for (int i = 0; i < numModules; i++) { int numStringsPerModule = sc.nextInt(); ArrayList<String> moduleEntries = new ArrayList<>(); for (int j = 0; j < numStringsPerModule; j++) { String entry = sc.next(); moduleEntries.add(entry); } modulesList.add(moduleEntries); } return modulesList; } 

You should be able to tailor this code to your specific situation. The general idea is that you will need to use a nested for loop, with the inner loop reading in a single module.

Sign up to request clarification or add additional context in comments.

2 Comments

need to? I doubt. I prefer using methods to parse substructures as the original poster tried.
There will still be an inner for loop; it is just hidden inside a method call. I am speaking from a algorithmic standpoint, not a semantical one. The big-O is still the same.
0

What about the following?

 public ArrayList<Student> loadStudents(Scanner infile) throws FileNotFoundException{ ArrayList<Student> students = new ArrayList<Student>(); 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); } return studtends; } 

And

public List<Module> loadModules(String fileName) throws FileNotFoundException{ Scanner infile =new Scanner(new InputStreamReader (new FileInputStream(fileName))); int num=infile.nextInt();infile.nextLine(); List<Module> ret = List<Module>(); for (int i=0;i<num;i++){ String c=infile.nextLine(); ArrayList<Student> a =loadStudents(infile); Module m = new Module(c,a); ret.add(m); } infile.close(); return ret; } 

The Module constructor would be better with List. Do not use implementations in interfaces if possible.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.