简体   繁体   English

为什么findInLine(token)有效但hasNext(token)不起作用

[英]Why findInLine(token) works but hasNext(token) doesn't

I am trying to understand the Scanner class and its methods. 我试图了解Scanner类及其方法。 I have understood how findInLine method works but I can't understand how hasNext(String) and next(String) methods work. 我已经了解findInLine方法如何工作,但是我不了解hasNext(String)next(String)方法如何工作。

Here's my code. 这是我的代码。

import java.util.Scanner;

public class ScannerTest {

    public static void main(String[] args) {

        hasNextTest("\\d");
        //findInLineTest("\\d");
    }

    public static void findInLineTest(String token) {
        Scanner s = new Scanner(System.in);
        System.out.println("Input source string :");
        System.out.flush();
        String str = null;
        while (( str = s.findInLine(token)) != null) {
            System.out.println(str);
        }
        System.out.flush();
        s.close();
    }

    public static void hasNextTest(String token) {
        Scanner s = new Scanner(System.in);
        System.out.println("Input source string :");
        System.out.flush();
        System.out.println(s.hasNext(token));
//      while (s.hasNext(token)) {

//          System.out.println(s.next(token));
//      }
        System.out.flush();
        s.close();
    }

}

The part I can't understand is if I pass to the scanner String "abcd12345abcd" or say any other String that has digits, the method hasNext("\\\\d") doesn't return true. 我无法理解的部分是,如果我将其传递给扫描程序字符串“ abcd12345abcd”或说任何其他具有数字的字符串,则hasNext("\\\\d")不会返回true。 It doesn't return true for any pattern and Strings for which the hasNext(String) method should return true. 它不return true的任何图案和字符串其中hasNext(String)方法应返回true。 I'm not using the Scanner object correctly but can someone help me find the mistake. 我没有正确使用Scanner对象,但是有人可以帮助我找到错误。

Thanks. 谢谢。

hasNext(pattern) attempts to match the specified pattern against entire tokens. hasNext(pattern)尝试将指定的模式与整个令牌进行匹配。 The string "abcd12345abcd" does not have delimiters, so it constitutes one token. 字符串“ abcd12345abcd”没有定界符,因此它构成一个标记。 Your pattern looks for a token consisting of a single digit, which obviously isn't going to match. 您的模式会寻找由一位数字组成的令牌,这显然不会匹配。

findInLine(pattern) , on the other hand, attempts to find a pattern anywhere in the line, so the first found digit would match. 另一方面, findInLine(pattern)尝试在行中的任何位置查找模式,因此找到的第一个数字将匹配。

暂无
暂无

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

相关问题 使用数组在Scanner类中的hasNext和findInLine - hasNext and findInLine in Scanner Class using array 为什么这个应该选择一些随机行的Hibernate HQL查询不起作用? 我正在获取“ QuerySyntaxException:意外令牌” - Why this Hibernate HQL query that should select some random rows doesn't works? I am obtaining an “QuerySyntaxException: unexpected token” 为什么我的 controller 没有向 Postman 返回令牌 - Why doesn't my controller return a token to Postman 为什么java.util.Collection不直接定义next(),hasNext()? - Why doesn’t java.util.Collection define next(), hasNext() directly? Java Scanner hasNext()不返回false - Java Scanner hasNext() doesn't return false 令牌“ &lt;”上的语法错误,无效的AssignmentOperator,在iterator.hasNext()中 - Syntax error on token “<”, invalid AssignmentOperator, in iterator.hasNext() 为什么hasNext()不会为假? - Why won't hasNext() become false? 为什么列表迭代器中 hasPrevious() 和 add() 的组合进入无限循环,而 hasNext() 和 add() 没有? - Why combination of hasPrevious() and add() in listiterator goes into infinite looping ,while hasNext() and add() doesn't? 为什么dbcursor.count()与hasNext-then-next迭代的时间不一致? - Why doesn't dbcursor.count() accord with the times of hasNext-then-next iteration? 为什么我看不到 ID 令牌或 Auth 令牌? - Why I don't see the ID Token or Auth Token?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM