| Summary: | Scanner return java.util.NoSuchElementException when a function finish execution | ||
|---|---|---|---|
| Product: | coworking space management system | Reporter: | yongjiawu5-c |
| Component: | system | Assignee: | yongjiawu5-c |
| Status: | RESOLVED FIXED | ||
| Severity: | normal | CC: | yongjiawu5-c |
| Priority: | --- | ||
| Version: | release1 | ||
| Hardware: | PC | ||
| OS: | Windows | ||
|
Description
yongjiawu5-c
2022-12-05 06:16:01 HKT
Receive the error. Working on it. Solved the issue.
Reason:
no need to close the scanner inside a called function. Otherwise the scanner in main function will be eliminated.
Solution:
remove the scanner closure command in CmdRegister class
Modified code:
package System;
import java.util.Scanner;
public class CmdRegister implements Command {
public CmdRegister() {}
public String[] getParamList() {
Scanner in = new Scanner(System.in);
String[] paramList = new String[2];
System.out.print("Username:");
paramList[0] = in.nextLine();
System.out.print("Password:");
paramList[1] = in.nextLine();
return paramList;
}
public void execute(Role role, String[] paramList) {
if(paramList!=null&&role.canRegister()) {
ManagementPortal managementPortal = ManagementPortal.getInstance();
managementPortal.register(paramList);
}
else if(paramList==null) {}
else {
System.out.print("Logged in user cannot register.\n");
}
}
}
|