简体   繁体   English

如何清除 Java 中的扫描仪缓冲区?

[英]How can I clear the Scanner buffer in Java?

I have something like this:我有这样的事情:

    Scanner in=new Scanner(System.in);
    int rounds = 0;
    while (rounds < 1 || rounds > 3) {
        System.out.print("How many rounds? ");
        if (in.hasNextInt()) {
            rounds = in.nextInt();
        } else {
            System.out.println("Invalid input. Please try again.");
            System.out.println();
        }
        // Clear buffer
    }
    System.out.print(rounds+" rounds.");

How can I clear the buffer?如何清除缓冲区?

Edit: I tried the following, but it does not work for some reason:编辑:我尝试了以下操作,但由于某种原因它不起作用:

while(in.hasNext())
    in.next();

Try this:试试这个:

in.nextLine();

This advances the Scanner to the next line.这使扫描仪前进到下一行。

You can't explicitly clear Scanner's buffer.您不能明确清除扫描仪的缓冲区。 Internally, it may clear the buffer after a token is read, but that's an implementation detail outside of the porgrammers' reach.在内部,它可能会在读取令牌后清除缓冲区,但这是 porgrammers 无法触及的实现细节。

Use the following command:使用以下命令:

in.nextLine();

right after紧接着

System.out.println("Invalid input. Please Try Again.");
System.out.println();

or after the following curly bracket (where your comment regarding it, is).或在以下大括号之后(您对此有何评论)。

This command advances the scanner to the next line (when reading from a file or string, this simply reads the next line), thus essentially flushing it, in this case.这个命令将扫描器推进到下一行(从文件或字符串读取时,这只是读取下一行),从而在这种情况下基本上刷新它。 It clears the buffer and readies the scanner for a new input.它清除缓冲区并为新的输入准备扫描仪。 It can, preferably, be used for clearing the current buffer when a user has entered an invalid input (such as a letter when asked for a number).当用户输入无效输入(例如要求输入数字时的字母)时,它可以优选地用于清除当前缓冲区。

Documentation of the method can be found here: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()该方法的文档可以在这里找到: http : //docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()

Hope this helps!希望这可以帮助!

This should fix it...这应该解决它...

Scanner in=new Scanner(System.in);
    int rounds = 0;
    while (rounds < 1 || rounds > 3) {
        System.out.print("How many rounds? ");
        if (in.hasNextInt()) {
            rounds = in.nextInt();
        } else {
            System.out.println("Invalid input. Please try again.");
            in.next(); // -->important
            System.out.println();
        }
        // Clear buffer
    }
    System.out.print(rounds+" rounds.");

Other people have suggested using in.nextLine() to clear the buffer, which works for single-line input.其他人建议使用in.nextLine()清除缓冲区,这适用于单行输入。 As comments point out, however, sometimes System.in input can be multi-line.然而,正如评论指出的那样,有时 System.in 输入可以是多行的。

You can instead create a new Scanner object where you want to clear the buffer if you are using System.in and not some other InputStream.如果您使用的是 System.in 而不是其他一些 InputStream,您可以创建一个新的 Scanner 对象,在其中清除缓冲区。

in = new Scanner(System.in);

If you do this, don't call in.close() first.如果你这样做,不要先调用in.close() Doing so will close System.in, and so you will get NoSuchElementExceptions on subsequent calls to in.nextInt();这样做将关闭 System.in,因此您将在后续调用in.nextInt();获得 NoSuchElementExceptions in.nextInt(); System.in probably shouldn't be closed during your program. System.in 可能不应该在您的程序中关闭。

(The above approach is specific to System.in. It might not be appropriate for other input streams.) (上述方法特定于 System.in。它可能不适用于其他输入流。)

If you really need to close your Scanner object before creating a new one, this StackOverflow answer suggests creating an InputStream wrapper for System.in that has its own close() method that doesn't close the wrapped System.in stream.如果您确实需要在创建新对象之前关闭 Scanner 对象,则此 StackOverflow 答案建议为 System.in 创建一个 InputStream 包装器,该包装器具有自己的 close() 方法,该方法不会关闭包装的 System.in 流。 This is overkill for simple programs, though.不过,这对于简单的程序来说太过分了。

scan.nextLine();

在读取字符串输入之前放置上面的行

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

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