我有2个类,即MyClient和CustomerUserInterface。
MyClient类具有一个主要方法,在其中我调用CustomerUserInterface的方法。

我的客户

    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);
        }
    }


客户用户界面

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);
    }
}
}


我面临的问题是在第一个System.out行之后,login()方法出现错误,即

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方法之后才显示错误。

最佳答案

您正在system.in上两次注册Scanner,一次是在MyClient中:

public class MyClient {
   public static void main(String args[]) {
     Scanner scan = new Scanner(System.in);


并在CustomerUserInterface登录方法中输入一次:

@Override
public void login() {
   Scanner scan = new Scanner(System.in);


那将不起作用,因为第一台扫描仪已经具有System.in流。

您需要在整个程序中使用相同的扫描仪实例。

10-06 06:02