我正在尝试使用扫描仪将一些数据从文本文件加载到 GUI。我的文本文件中有两个部分:俱乐部和会员。代码在俱乐部部分运行正常。例如,如果我的列表中有 4 个俱乐部,则会显示所有俱乐部,但对于“成员”部分,无论列表中有多少成员,都只会显示第一个成员。这是我的代码:
public void load (String fileName) throws FileNotFoundException {
FileInputStream fileIn = new FileInputStream("Clubs.txt");
Scanner scan = new Scanner(fileIn);
while (scan.hasNextLine()){
String line = scan.nextLine();
if(line.equals("Members")){
String firstName = scan.next();
String lastName = scan.next();
Pupil p1 = new Pupil( firstName, lastName);
pupils[nbrPupils] = p1;
nbrPupils ++;
}
else if(line.equals("Clubs")){
while (scan.hasNext()){
String club = scan.nextLine();
Club aNewClub = new Club(club);
clubs[nbrClubs] = aNewClub;
nbrClubs ++;
}
}