我正在制作一本成绩簿,用于存储学生和教师,每个学生和教师都有一个唯一的ID,以及他们在学生和教师对象中注册或教授ArrayList的各自课程。我有文件夹路径" J:/ compsci / Classes /"存储每个类的文本文件。
文本文件格式:
第1行:班级名称,教师ID,期间,荣誉?,班级ID
第2行:以逗号分隔的班级中每个学生的学生ID。
我在这里初始化每个学生正在学习的所有课程,每个老师正在教授的所有课程,以及每个班级的所有学生和信息。
基于我迄今为止所学到的一切,我在本地范围内重新定义了类对象c for-loop因此它应该是一个全新的对象,最初没有学生,但在下面的行中,它包括来自所有以前类的学生在打印时进行调试。
请帮助我花了一周的时间来解决这个问题
调试代码
File[] classes = new File("J:/compsci/Classes/").listFiles();
for (File currentClass: classes) {
System.out.println(currentClass.getPath());//proves that i'm moving to a different class
try (Scanner fileReader = new Scanner (new FileInputStream (currentClass.getPath()))){
String[] classInfo= fileReader.nextLine().split(",");
for (String s: classInfo)//all the info going into the parameters for a new class
System.out.print(s+",");
System.out.println();
Class c = new Class (classInfo[0],classInfo[1],Integer.parseInt(classInfo[2]),classInfo[3].equals("H"),currentClassID());
for (Student s: c.getStudents())//PROBLEM!! should always be empty initially and not store students from previous classes!!
System.out.print(s.getID()+",");
System.out.println();
String[] studentslist= fileReader.nextLine().split(",");
for (String s: studentslist)
c.addStudent((Student)getUserFromID(s));
((Teacher)getUserFromID(classInfo[1])).addClass(c);
for (String s: studentslist) {//prints the only students that are actually supposed to be in the class
System.out.println(s);
((Student)getUserFromID(s)).addClass(c);
}
CLASSLIST.add(c);//global arraylist storing all classes
} catch (Exception e) {
e.printStackTrace();
}
}
控制台输出
J:\compsci\Classes\APBiology.txt
AP Biology,00009,5,H,00001,
00001
00003
00004
00011
J:\compsci\Classes\Chinese5AP.txt
Chinese 5 AP,00007,0,H,00002,
00001,00003,00004,00011,
00005
00011
J:\compsci\Classes\ComputerScienceA.txt
Computer Science A,00007,2,H,00003,
00001,00003,00004,00011,00005,00011,
00001
00002
00003
00004
00005
J:\compsci\Classes\ComputerSciencePrinciples.txt
Computer Science Principles,00007,3,H,00004,
00001,00003,00004,00011,00005,00011,00001,00002,00003,00004,00005,
00003
00004
00005
00011
00001
没有任何调试的代码
File[] classes = new File("J:/compsci/Classes/").listFiles();
for (File currentClass: classes) {
try (Scanner fileReader = new Scanner (new FileInputStream (currentClass.getPath()))){
String[] classInfo= fileReader.nextLine().split(",");
String[] studentslist= fileReader.nextLine().split(",");
Class c = new Class (classInfo[0],classInfo[1],Integer.parseInt(classInfo[2]),classInfo[3].equals("H"),currentClassID());
((Teacher)getUserFromID(classInfo[1])).addClass(c);
for (String s: studentslist)
c.addStudent((Student)getUserFromID(s));
for (String s: studentslist)
((Student)getUserFromID(s)).addClass(c);
CLASSLIST.add(c);//global arraylist storing all classes
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
由于您在创建新的C类后发生了问题:
Class c = new Class (classInfo[0],classInfo[1],Integer.parseInt(classInfo[2]),classInfo[3].equals("H"),currentClassID());
for (Student s: c.getStudents())//PROBLEM!! should always be empty initially and not store students from previous classes!!
但是你没有附上你的Class构造函数,我猜你错过了重置List的学生attriburte。请添加
students = new ArrayList<>();
到Class构造函数。
答案 1 :(得分:0)
是的,所以显然我把我的类对象中的arraylist作为静态oops。没有意识到搞砸了一切