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

时间:2022-08-26 22:44:12

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();

7 个解决方案

#1


7  

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的范围之外的实现细节。

#2


27  

Try this:

尝试这个:

in.nextLine();

This advances the Scanner to the next line.

这使扫描仪进入下一行。

#3


5  

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.");

#4


5  

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!

希望这可以帮助!

#5


0  

Insert the line

插入行

in.nextLine();

#6


0  

Try this code:

试试这段代码:

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 {
             in.NextLine(); // to clear Scanner
      //You can  throw new InputMismatchException();
            System.out.println("Invalid input. Please try again.");
            System.out.println();
        }

    }
    System.out.print(rounds+" rounds.");

#7


0  

One solution is to place the bank object inside the do-while loop in main so that a new bank object is created each time.

一种解决方案是将bank对象放在main中的do-while循环内,以便每次都创建一个新的bank对象。

However this presents certain issues, namely the account array and the count variable. You can fix this by placing them in bankUser.

然而,这提出了某些问题,即帐户数组和计数变量。您可以通过将它们放在bankUser中来解决此问题。

static int count = 0;  //don't forget to make count global and initialize
public static void main(String[] args) {


    int Choice; 
    //place the Acct array inside bankUser main or declare
    //as global
    bankAcct myAcct[] = new bankAcct[3]; 

    do
    { //Place the bank object inside the do-while
    Bank myBank = new Bank(count, myAcct); //pass the variables to the 
                                           //new Bank object
    dispMenu();

    Choice = getChoice();

    proChoice(Choice, myBank);

    }   
    while (Choice !=0);
   }

Don't forget to make count global because it needs to be able to be passed into the object in main And incremented by the switch-case openAcct in the proChoice method.

不要忘记将count设为全局,因为它需要能够传递到main中的对象并且通过proChoice方法中的switch-case openAcct递增。

case 1: myBank.openAcct();
    count++;
    break;

Finally you can pass the myAcct array and the count variable through the object using a constructor in the bank class.

最后,您可以使用bank类中的构造函数将myAcct数组和count变量传递给对象。

public Bank(int count, bankAcct myAcct[]) {
this.count = count;
this.myAcct = myAcct;

}

}

#1


7  

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的范围之外的实现细节。

#2


27  

Try this:

尝试这个:

in.nextLine();

This advances the Scanner to the next line.

这使扫描仪进入下一行。

#3


5  

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.");

#4


5  

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!

希望这可以帮助!

#5


0  

Insert the line

插入行

in.nextLine();

#6


0  

Try this code:

试试这段代码:

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 {
             in.NextLine(); // to clear Scanner
      //You can  throw new InputMismatchException();
            System.out.println("Invalid input. Please try again.");
            System.out.println();
        }

    }
    System.out.print(rounds+" rounds.");

#7


0  

One solution is to place the bank object inside the do-while loop in main so that a new bank object is created each time.

一种解决方案是将bank对象放在main中的do-while循环内,以便每次都创建一个新的bank对象。

However this presents certain issues, namely the account array and the count variable. You can fix this by placing them in bankUser.

然而,这提出了某些问题,即帐户数组和计数变量。您可以通过将它们放在bankUser中来解决此问题。

static int count = 0;  //don't forget to make count global and initialize
public static void main(String[] args) {


    int Choice; 
    //place the Acct array inside bankUser main or declare
    //as global
    bankAcct myAcct[] = new bankAcct[3]; 

    do
    { //Place the bank object inside the do-while
    Bank myBank = new Bank(count, myAcct); //pass the variables to the 
                                           //new Bank object
    dispMenu();

    Choice = getChoice();

    proChoice(Choice, myBank);

    }   
    while (Choice !=0);
   }

Don't forget to make count global because it needs to be able to be passed into the object in main And incremented by the switch-case openAcct in the proChoice method.

不要忘记将count设为全局,因为它需要能够传递到main中的对象并且通过proChoice方法中的switch-case openAcct递增。

case 1: myBank.openAcct();
    count++;
    break;

Finally you can pass the myAcct array and the count variable through the object using a constructor in the bank class.

最后,您可以使用bank类中的构造函数将myAcct数组和count变量传递给对象。

public Bank(int count, bankAcct myAcct[]) {
this.count = count;
this.myAcct = myAcct;

}

}