我正在尝试运行使用输入的多种方法,但会收到扫描仪的例外

发布于 2025-02-04 02:20:25 字数 2946 浏览 3 评论 0原文

我通过删除s.close()调用来解决此问题,所以我可以忽略eclipse抱怨内存泄漏吗?为什么创建一个新的扫描仪对象不重新打开system.in


以下错误是完整的错误:

Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at com.qa.jdbc.table.CustomerDAO.deleteCustomerByID(CustomerDAO.java:109)
at com.qa.jdbc.runner.Runner.main(Runner.java:16)

这是我的跑步者:

package com.qa.jdbc.runner;

import com.qa.jdbc.table.Customer;
import com.qa.jdbc.table.CustomerDAO;

public class Runner {

    public static void main(String[] args) {
        CustomerDAO customerDAO = new CustomerDAO("root", "root");
        Customer customer = new Customer();
        
        customer.createCustomer(customer);
        customerDAO.deleteCustomerByID();
        customerDAO.updateCustomer(customer);
        System.out.println(customerDAO.readAll());
    }
}

这是customer&的方法。 customerdao相关的类文件:

public Customer createCustomer(Customer customer) {
        
        Scanner s = new Scanner(System.in);
        try {
            System.out.println("Enter Customer First Name:");
            customer.setFirstName(s.nextLine());
            System.out.println("Enter Customer Last Name:");
            customer.setLastName(s.nextLine());
            System.out.println("Enter Customer Home Address:");
            customer.setHomeAddress(s.nextLine());
            return customer;
        } finally {
            s.close();
            System.out.println("Scanner Closed!");
        }
    }
public void deleteCustomerByID() {
        Scanner s = new Scanner(System.in);
        int customer_id;
        String deleteQuery = "DELETE FROM customers WHERE customer_id = ?";
        try {
            Connection con = DriverManager.getConnection(jdbcConnectionURL, username, password);
            PreparedStatement ps = con.prepareStatement(deleteQuery);
            
            System.out.println("Enter Customer ID of customer to delete:");
            customer_id = s.nextInt();
            ps.setInt(1, customer_id);
            int rows = ps.executeUpdate();
            System.out.println("Rows affected: " + rows);
        } catch (SQLException e) {
            LOGGER.debug(e.getStackTrace());
        } finally {
            s.close();
            System.out.println("Scanner Closed!");
        }
    }

使用hasnextline()方法是在此处的正确调用或其他类似Switch-case的内容,以便选择用户希望的方法表演?

在获取用户输入后的控制台输出

I fixed this by removing the s.close() calls, so can I just ignore Eclipse complaining about the memory leak? Why does creating a new Scanner object not re-open System.in?


The following error is the full error:

Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at com.qa.jdbc.table.CustomerDAO.deleteCustomerByID(CustomerDAO.java:109)
at com.qa.jdbc.runner.Runner.main(Runner.java:16)

This is my Runner:

package com.qa.jdbc.runner;

import com.qa.jdbc.table.Customer;
import com.qa.jdbc.table.CustomerDAO;

public class Runner {

    public static void main(String[] args) {
        CustomerDAO customerDAO = new CustomerDAO("root", "root");
        Customer customer = new Customer();
        
        customer.createCustomer(customer);
        customerDAO.deleteCustomerByID();
        customerDAO.updateCustomer(customer);
        System.out.println(customerDAO.readAll());
    }
}

Here is the methods in the Customer & customerDAO Class Files that are relevant:

public Customer createCustomer(Customer customer) {
        
        Scanner s = new Scanner(System.in);
        try {
            System.out.println("Enter Customer First Name:");
            customer.setFirstName(s.nextLine());
            System.out.println("Enter Customer Last Name:");
            customer.setLastName(s.nextLine());
            System.out.println("Enter Customer Home Address:");
            customer.setHomeAddress(s.nextLine());
            return customer;
        } finally {
            s.close();
            System.out.println("Scanner Closed!");
        }
    }
public void deleteCustomerByID() {
        Scanner s = new Scanner(System.in);
        int customer_id;
        String deleteQuery = "DELETE FROM customers WHERE customer_id = ?";
        try {
            Connection con = DriverManager.getConnection(jdbcConnectionURL, username, password);
            PreparedStatement ps = con.prepareStatement(deleteQuery);
            
            System.out.println("Enter Customer ID of customer to delete:");
            customer_id = s.nextInt();
            ps.setInt(1, customer_id);
            int rows = ps.executeUpdate();
            System.out.println("Rows affected: " + rows);
        } catch (SQLException e) {
            LOGGER.debug(e.getStackTrace());
        } finally {
            s.close();
            System.out.println("Scanner Closed!");
        }
    }

Would using the hasNextLine() method be the right call here or something else like a switch-case in order to choose what method a user wishes to perform?

Console output after taking user input

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文