当前位置: 首页 > 知识库问答 >
问题:

爪哇;当我遇到一个输入时,我会得到“Exception in thread”main“java.util.NoSuchelementException”

益智明
2023-03-14

我正在制作一个非常基本的数据库管理程序,我有一个名称和值扫描器在一个类(account.java)中运行良好,

public static void createNewAccount() {

    Scanner newScan = new Scanner(System.in);
    System.out.print("Enter name: ");
    String inputName = newScan.nextLine();
    name = inputName;
    System.out.print("Enter the initial value: ");
    double inputBalance = newScan.nextInt();
    System.out.println("... successfully created new account.");

    keyScan.close();


    Account newAccount = new Account(inputName, inputBalance);

}
...
Scanner in = new Scanner(System.in);

    Account.createAccount();
//Debugging statement #1 here works fine, can access other Class
do{
        /*
        Menu Options
        */
        System.out.println(">> Select your option (1-6)");
        choice = in.nextInt();

//Debugging statement #2 here doesn't display, this is where the error hits.

    switch (choice) {
         ... Cases
        }
>> Select your option (1-6)
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)
        at Management.main(Management.java:30)

我很困惑为什么会发生这种情况,因为我有另一个扫描仪,工作得很好。

共有1个答案

杜霍英
2023-03-14

当关闭扫描程序时,它也会关闭基础的InputStream。因此,如果在程序的早期使用system.in创建扫描程序,然后关闭该扫描程序,它将关闭system.in

一旦关闭了inputstream,就不能再从中读取它,尝试这样做将导致NoSuchelementException

 类似资料:
  • 我试图在返回Object的方法中简单地从用户获得输入。由于某种原因,将引发此错误: 线程“main”java.lang.reflect.invocationTargetException在java.base/jdk.internal.reflect.nativeMethodAccessorImpl.invoke0(原生方法)在java.base/jdk.internal.reflect.nativ

  • AMQ6235E:缺少目录“H:”。 我无法找到解决这个问题的方法,它正在连接队列,它显示了这个错误。我不能公开我运行的代码,但我可以解释节点脚本将做什么,首先尝试连接队列管理器,然后尝试通道名,主机名,端口名,这里问题是当它需要建立连接而不是得到上面的错误

  • 我还尝试了,,JMeter获得了多达8000个示例,最大时间12000ms(超时30s),并给出了错误。在拒绝之前,它至少应该排队10,000个连接。

  • 正如你所看到的,我得到了这个错误。和我的包裹有关。json?默认值如下所示。 在我执行命令npm rm-g create-react-app后,我在命令提示符中得到以下内容

  • 以下是错误: 警告:需要(/home/****/public_html/wp-包含/load.php):无法打开流:第21行 /home/growfi5/public_html/wp-settings.php中没有这样的文件或目录 致命错误:require():无法打开required'/home/**/public\u html/wp includes/load。php'(include_pat

  • 我正在处理laravel,当我尝试迁移一个表时遇到了这个问题,这会带来一个旧错误,即存在一个我迁移了几个月的表。现在,当我想创建会话表来容纳用户会话时,它不会迁移,而且我还遇到了路由问题。 它说: 尝试读取会话数据时引发PDOException:SQLSTATE[42S02]:找不到基表或视图:1146表“EUAEL.sessions”不存在 当我要迁移会话表时,会出现以下错误: PHP致命错误: