У меня есть 2 класса, а именно, MyClient и CustomerUserInterface. Класс MyClient имеет основной метод, в котором я вызываю методы CustomerUserInterface.
MyClient
public class MyClient {
public static void main(String args[]) {
CustomerUserInterface customerUserInterface = new CustomerUserInterfaceImpl();
Scanner scan = new Scanner(System.in);
customerUserInterface.registerLoginMenu();
int choice = scan.nextInt();
customerUserInterface.performOperationsOnRegisterLoginMenu(choice);
}
}
CustomerUserInterface
public class CustomerUserInterfaceImpl implements CustomerUserInterface {
private CustomerBL customerBl;
private ProductInterface productInterface;
public CustomerUserInterfaceImpl() {
customerBl = new CustomerBLImpl();
productInterface = new ProductInterfaceImpl();
}
@Override
public void registerLoginMenu() {
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("3. Exit");
}
@Override
public void register() {
Customer customer = CustomerInputHelper.inputCustomer();
boolean status=false;
try {
status = customerBl.registerUser(customer);
}catch (SQLException e) {
e.printStackTrace();
}
if(status) {
System.out.println("Register Success");
login();
}
else {
System.out.println("Register Unsuccessful");
registerLoginMenu();
}
}
@Override
public void login() {
Scanner scan = new Scanner(System.in);
Boolean status=false;
System.out.println("Enter customerID : ");
int id = scan.nextInt();
System.out.println("Enter password : ");
String pwd = scan.next();
try {
status = customerBl.verifyUser(id, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(status) {
productInterface.afterLoginMenu();
System.out.println("Enter choice : ");
int choice = scan.nextInt();
Customer customer = null;
try {
customer = customerBl.getCustomer(id);
} catch (SQLException e) {
e.printStackTrace();
}
productInterface.performOperationsOnAfterLoginMenu(choice,customer);
}
else {
System.out.println("Login failed");
registerLoginMenu();
}
scan.close();
}
@Override
public void performOperationsOnRegisterLoginMenu(int choice) {
switch(choice) {
case 1:
register();
break;
case 2:
login();
break;
case 3:
System.out.println("Good Bye ! Have a Nice Day!");
System.exit(0);
}
}
}
Проблема, с которой я сталкиваюсь, заключается в том, что я получаю ошибку в методе login () после моей первой строки System.out, т.е.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
Регистр вызывает метод login после успешной регистрации, но простопосле вызова метода входа в систему я получаю вышеуказанную ошибку.
Я не понимаю проблему, поскольку пользователь регистрируется, но ошибка отображается сразу после вызова метода входа в систему.