简体   繁体   English

扫描仪上的java.util.NoSuchElementException

[英]java.util.NoSuchElementException on scanner

I have a scanner called input with this code: 我有一个名为input的扫描器,其代码如下:

Scanner input = new Scanner(System.in);
    int cFreq = 0;

    System.out
            .println("Enter the symbol which you want to find the frequency of:");
    char s = 'a';
    symbolLoop: while (s == 'a') {
        try {
            s = input.next(".").toLowerCase().charAt(0);
        } catch (InputMismatchException e) {
            System.out.println("Please enter a valid symbol!");
            input.next();
        }
        switch (checkSymbol(s)) {
        case 0:
            s = 'a';
            break;
        case 1:
            break symbolLoop;
        }
    }
    for (int i = 0; i < words.size(); i++) {
        cFreq += words.get(i).find(s, true);
    }
    System.out.println("Number of times " + s + " is in the coded words: "
            + cFreq);
}

However when it reaches the line where it reads from the scanner it terminates with this error: 但是,当它到达从扫描仪读取的行时,它将终止,并显示以下错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.next(Scanner.java:1418)
at Home.frequency(Home.java:118)
at Menu.select(Menu.java:58)
at Menu.view(Menu.java:17)
at Home.main(Home.java:22)

(Home is the name of the class). (家是班级的名称)。 I have no idea what is causing this and would appreciate some help! 我不知道是什么原因造成的,不胜感激! :) Thanks! :) 谢谢! EDIT: I have also tried with this code: String str = input.nextLine(); 编辑:我也尝试使用此代码: String str = input.nextLine(); in the same method but it throws the same error. 以相同的方法,但会引发相同的错误。

If data from user wouldn't match "." 如果来自用户的数据与"."不匹配"." regex (possibly surrounded by delimiters - whitespaces) next(".") would't throw NoSuchElementException , not InputMismatchException . 正则表达式(可能被定界符-空格包围) next(".")不会抛出NoSuchElementException ,而不是InputMismatchException

NoSuchElementException exception is thrown when Scanner can't even hope for more data, in which case it is sure that potentially next element will not even exist. 当Scanner甚至不希望有更多数据时,将引发NoSuchElementException异常,在这种情况下,可以确保可能甚至不存在下一个元素。 Such situation is only possible if Scanner knows that source of data will not have more elements, which in case of Stream is when we will read entire content of stream (like in case of FileInputStream) or it will be closed. 这种情况只有在Scanner知道数据源将没有更多元素的情况下才有可能,在Stream的情况下,这是我们将读取流的全部内容(例如在FileInputStream的情况下)或将其关闭的情况。

In case of System.in we can assume that content of this stream is infinite, because it is provided by user, so when we try to read from it, application is waiting until user will provide its data. 对于System.in我们可以假定此流的内容是无限的,因为它是由用户提供的,因此,当我们尝试从中读取内容时,应用程序将等待直到用户提供其数据为止。 Which means that only way Scanner would throw NoSuchElementException is when System.in was closed, so next doesn't have more data to read. 这意味着Scanner抛出NoSuchElementException的唯一方法是关闭System.in时,因此next没有更多的数据可读取。
To avoid such cases you should avoid closing streams which are using System.in because you will not be able to reopen it again in your application. 为了避免这种情况,您应该避免关闭正在使用System.in流,因为您将无法在应用程序中再次将其重新打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM