java - 由于 nextInt() 导致的 java.util.NoSuchElementException 类型的异常

标签 java eclipse java.util.scanner

我有 2 个类,即 MyClient 和 CustomerUserInterface。 MyClient 类有一个 main 方法,我在其中调用 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)

注册成功后,注册器调用登录方法,但在登录方法调用后,我收到上述错误。

我不明白这个问题,因为用户注册了,但在调用登录方法后立即显示错误。

最佳答案

您在 system.in 上注册扫描程序两次,在 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 流。

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

关于java - 由于 nextInt() 导致的 java.util.NoSuchElementException 类型的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58260682/

相关文章:

java SimpleDateFormat 返回 Windows 上的部分时间

java - 以编程方式将 Xtend 类设置为 Java 类的父类(super class)

java - 使用 While 循环、使用扫描仪输入变量

Java 扫描器和 if-else 语句

java - 以接口(interface)为参数调用私有(private)方法

java - 如何解析字符串中的日期并设置为新格式

java - Android 通过 USB 传输文件

java - 如何协调maven开发目录结构与maven发布目录结构

eclipse - 启动 Eclipse 导致无法创建 Java 虚拟机

java - 将 Java 堆栈与字符串元素结合使用