【发布时间】:2014-02-02 19:38:08
【问题描述】:
我正在尝试使用扫描仪加载模块的 ArrayList,然后每个模块还包含一个为该模块注册的学生的 ArrayList。这是我的模块构造函数:
public Module(String newCode, ArrayList<Student> students){
}
数据将从一个布局如下的文本文件中加载:
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
我已经能够像这样使用扫描仪加载学生:
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();
}
但我正在努力加载其中包含 ArrayLists 的 ArrayList,这就是我当前的粗略代码的样子:
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);
但这显然行不通。这里的任何想法或提示将不胜感激
【问题讨论】:
-
您确定要复制粘贴相同的代码吗?导致
ArrayList<Student> a = infile.next();这一行将无法编译。 -
是的,这就是它不起作用的问题。这是我试图让它工作的尝试,但显然它不起作用。
-
请正确复制粘贴
loadModules代码。 -
这就是 loadModules 的全部内容,我不知道它应该是什么样子