而(!scannerObject.hasNext(patternObject))仅适用于一次迭代 [英] while (!scannerObject.hasNext(patternObject)) works for only one iteration

查看:57
本文介绍了而(!scannerObject.hasNext(patternObject))仅适用于一次迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我只能从受hasNext(Pattern pattern)条件控制的while循环中获得一次成功的迭代.我尝试使用.next()清除"输入流(?),但未成功.

I only get one successful iteration from a while loop that is controlled by a hasNext(Pattern pattern) condition. I try to 'clear' the input stream (?) with .next() but unsuccessfully.

行为

如果我是第一次输入无效的输入,则看似有效的后续输入将继续执行System.out.print("You did not enter a valid size. Try again: ")行.但是,仅当Scanner getPizzaSize没有有效输入时,才应执行此行.

If I enter invalid input the first time, seemingly valid subsequent input continues to execute the System.out.print("You did not enter a valid size. Try again: ") line. But this line should only execute if Scanner getPizzaSize does not have valid input.

如果我是第一次输入有效输入,它会起作用并且程序会前进,但是当while (orderingPizza)进行第二次迭代时,所有对getPizzaSize的输入(无论是否有效)都将执行System.out.print("You did not enter a valid size. Try again: ")行.

If instead I enter valid input the first time, it works and the program advances, but when while (orderingPizza) comes around for a second iteration all input to getPizzaSize (valid or not) executes System.out.print("You did not enter a valid size. Try again: ") line.

我从!getPizzaSize.hasNext(validPizzaSizes)循环中仅获得一次成功的迭代:

I only get one successful iteration from my !getPizzaSize.hasNext(validPizzaSizes) loop:

Please place the order for your pizza.
Size (s = small, l = large, f = family): s
Please place the order for your pizza.
Size (s = small, l = large, f = family): s
You did not enter a valid size. Try again: 

我应该得到:

Please place the order for your pizza.
Size (s = small, l = large, f = family): s
Please place the order for your pizza.
Size (s = small, l = large, f = family): s
Please place the order for your pizza.
Size (s = small, l = large, f = family):

(我使用的正则表达式应匹配: s,S,l,L,f,F .此链接可能是测试结果:https://regexr.com/5gdla. )

(The regular expression I am using should match: s, S, l, L, f, F. This link might be to test results: https://regexr.com/5gdla.)

解决方案

如果我在.hasNext(Pattern pattern)循环中重新声明Scanner getPizzaSize,则该循环的行为符合预期:

If I redeclare Scanner getPizzaSize in the .hasNext(Pattern pattern) loop, the loop behaves as expected:

  1. 无效输入后跟You did not enter valid input . . . try again;和
  2. 有效输入会通过程序进行

我想了解的

我从getPizzaSize中读取.next()的方式似乎存在问题.但是我不知道问题是什么.是我的正则表达式吗?关于.next().hasNext(Pattern pattern)结合的性质?对于整数输入,.hasNextInt().next()可以完美地工作.

There seems to be an issue with how I am reading in from getPizzaSize with .next(). But I don't know what the issue is. Is it my regular expression? Something about the nature of .next() in combination with .hasNext(Pattern pattern)? For integer inputs, .hasNextInt() and .next() work perfectly.

import java.util.Scanner;
import java.util.regex.Pattern;

public class PizzaOrder{

    public static void main(String[] args) {
        // Initialise Scanner object.
        Scanner getPizzaSize = new Scanner(System.in);

        // Initialise Pattern object for detecting valid size input.
        Pattern validPizzaSizes = Pattern.compile("^[slf]$", Pattern.CASE_INSENSITIVE);

        while (true) {
            // Ask for pizza size.
            System.out.print("Please place the order for your pizza.\n" +
                    "Size (s = small, l = large, f = family): ");
            // Validate size input.
            while (!getPizzaSize.hasNext(validPizzaSizes)) {
                System.out.print("You did not enter a valid size. Try again: ");
                getPizzaSize.next();
            }
            // Set pizza size.
            String pizzaSize = getPizzaSize.next();
        }
    }
}
            

推荐答案

您的正则表达式说的是"s","l"或"f"必须是字符串的开头和结尾,如^$锚点所示.但是,就扫描仪而言,您输入的第二个s不在字符串的开头.这是您输入的第一个s之后的 (可能也由行分隔符分隔了!)!

Your regex is saying that "s", "l" or "f" must be the start and the end of string, as indicated by the ^ and $ anchors. However, as far as the scanner is concerned, the second s you entered is not at the start of the string. It's after the first s you entered (probably also separated by a line separator)!

您不应使用^锚点.您的正则表达式应为:

You should not use the ^ anchor. Your regex should just be:

[slf]$

这也说明了创建新扫描仪的原因.因为新的扫描仪不知道您输入的先前s.

This also explains why creating a new scanner works. Because the new scanner doesn't know about the previous s that you entered.

这篇关于而(!scannerObject.hasNext(patternObject))仅适用于一次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆