没有这样的元素与扫描仪一起工作

发布于 2025-01-21 18:48:05 字数 1224 浏览 5 评论 0原文

我在使用扫描仪时遇到了问题,但我不知道如何解决它,这是Java中的新事物。我创建了一个菜单来读取用户通过扫描仪提供的输入字符,并且我从菜单中读取此循环的选项:

/**
 * Constant to exit the menu.
 */
private static final char EXIT = 'E';

public void run() {
    char option = EXIT;
    do {
        menu.show();
        option = menu.readOption();
        
        try {
            processOption(option);
        } catch (RuntimeException exception) {
            handleSystemError(exception);
        } catch (Exception exception) {
            handleUserError(exception);
        }
    } while (option != EXIT);
    
}

这是ReadOption的方法:

public char readOption() {
    return Console.readChar(" Option ");
}

这就是方法ReadChar的方法:

public static char readChar(String msg) {
    out.println( msg + ": ");
    keyboard.useDelimiter(System.lineSeparator());
    char res = keyboard.next().charAt(0);
    keyboard.reset();
    return res;
}

问题是,问题是,问题是。首次表演中的循环效果很好。该程序可用。但是,在第一个表演再次开始时,它在读取的线上,它给诺森氏菌Exception带来了很多时间,我已经尝试解决它了很多小时,我不知道该怎么做。显然,问题在方法中,当我尝试执行“键盘.next()”时,它会引发没有这样的元素异常。无法解释它是第一次有效的,而不是第二次。当第一次演出时,它会等待我的期望,但是在第二个表演中,但是在第二个表演中,我希望在“键盘.next()”时希望引入字符,但它会引发上述异常。我的扫描仪清楚地有一个问题,希望有人可以在这里确定这个问题。非常感谢大家。

I am having a problem while using a Scanner and I do not know how to solve it, kind of new in Java. I have created a menu to read an input char provided by the user through a Scanner and I read options from the menu with this loop:

/**
 * Constant to exit the menu.
 */
private static final char EXIT = 'E';

public void run() {
    char option = EXIT;
    do {
        menu.show();
        option = menu.readOption();
        
        try {
            processOption(option);
        } catch (RuntimeException exception) {
            handleSystemError(exception);
        } catch (Exception exception) {
            handleUserError(exception);
        }
    } while (option != EXIT);
    
}

This is what the method readOption does :

public char readOption() {
    return Console.readChar(" Option ");
}

And this is what the method readChar does :

public static char readChar(String msg) {
    out.println( msg + ": ");
    keyboard.useDelimiter(System.lineSeparator());
    char res = keyboard.next().charAt(0);
    keyboard.reset();
    return res;
}

The problem is, the loop in its first performance works perfectly fine. The program works propperly. But after the first performance, when it starts again, in the line where it readsOption, it throws a NoSuchElementException and I have been many hours trying to solve it and I do not know how to. Apparently the problem is in the method readChar, when I try to do "keyboard.next()" it throws the no such element exception. Cannot explain that it works the first time and not a second one. When first performance, it waits for me to introduce a char as I expect, but in the second one,while I wish to be expected to introduce a char, when "keyboard.next()", it throws the mentioned exception. Having an issue clearly with my Scanner, I hope someone can identify the issue here. Thank you very much everyone.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我要还你自由 2025-01-28 18:48:05

我终于找到了如何解决该页面的方法: solucione el error errory nosuchelementException en java

问题是,由于我必须同时使用2个扫描仪的大小,并且当我关闭其中一个时,这导致另一个停止工作,抛出了例外。

I finally found how to solve it thanks to this page: Solucione el error NoSuchElementException en Java

The issue was that because of the size of this program I had to use 2 scanners at the same time, and when I closed one of them, it caused the other one to stop working, throwing that exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文