我可以从用户那里获得scan2扫描仪的输入。但是在这段代码中没有生成扫描,我怎么能进入这个循环我无法从用户那里获得扫描扫描仪的输入。如何从用户那里获得扫描扫描仪的输入。
Scanner scan2 = new Scanner(System.in);
System.out.println("Register Section\nID: ");
String id = scan2.next();
System.out.println("Password: ");
String pass = scan2.next();
createAccount(id, pass, accounts);
System.out.println("Login Section\nID: ");
id = scan2.next();
System.out.println("Password: ");
pass = scan2.next();
boolean X = logIN(id, pass, students);
scan2.close();
//Login succeed
if(X){
Scanner scan = new Scanner(System.in);
String line = "";
while (scan.hasNextLine()) {
line = scan.nextLine();
if (line.equalsIgnoreCase("quit"))
System.exit(0);
int timetablestart = line.indexOf('[');
int timetablefinish = line.indexOf(']');
String strlistoftimetable = "";
String command = line;
String[] timetableslots = null;
if ((timetablestart > 0) && (timetablefinish > 0)) {
strlistoftimetable = line.substring(timetablestart + 1,
timetablefinish);
timetableslots = strlistoftimetable.split(", ");
command = line.substring(0, timetablestart - 1);
}
String[] tokens = command.split("\\s");
if (tokens.length < 2) {
System.out.println("something is wrong!");
return;
}
if (tokens[0].equals("add")) {
if (tokens[1].equals("O")) {
CSDriver.addO(tokens,timetableslots,departments);
} else if (tokens[1].equals("C")) {
CSDriver.addC(tokens, students);
} else if (tokens[1].equals("I")) {
CSDriver.addI(tokens,instructors);
}
}
else if (tokens[0].equals("print")) {
if (tokens[1].equals("S")) {
CSDriver.printS(tokens, students);
} else if (tokens[1].equals("I")) {
CSDriver.printI(tokens,instructors);
}
else if (tokens[1].equals("Announcement")) {
CSDriver.printAnnouncement(departments);
}
else if (tokens[1].equals("AcedemicActivities")) {
CSDriver.printAcedemicActivities(departments);}
else if (tokens[1].equals("Attendance")) {
CSDriver.checkAttendance(tokens, students);
}
else if (tokens[1].equals("Internship")) {
CSDriver.checkInternship(tokens, students);
}
else if (tokens[1].equals("SemesterGrades")) {
CSDriver.checkSemesterGrades(tokens, students);
}
} else
System.out.println("something is wrong!");
}
scan.close();
} }
答案 0 :(得分:3)
您没有从scan
获得任何内容,因为您已经关闭System.in
(在关闭scan2
时,在“登录成功”部分结束时)。您不应该自己关闭它,因为它会阻止将来尝试从此流中读取。
答案 1 :(得分:0)
使用时
Scanner scan2 = new Scanner(System.in);
scan2.close();
您正在关闭 scan2 ,但 System.in ,因此为了解决您的问题,您需要在程序结束时关闭扫描仪。
在最后移动此行
scan2.close();