java hasnext_Java扫描仪的hasNext()方法与示例

java hasnext

扫描器类的hasNext()方法 (Scanner Class hasNext() method)

Syntax:

句法:

    public boolean hasNext();
    public boolean hasNext(Pattern patt);
    public boolean hasNext(String patt);

  • hasNext() method is available in java.util package.

    hasNext()方法在java.util包中可用。

  • hasNext() method is used to check whether this Scanner has any other token exists in its input or not.

    hasNext()方法用于检查此扫描程序的输入中是否存在其他令牌。

  • hasNext(Pattern patt) method is used to check whether the next complete token meets the given pattern or not.

    hasNext(Pattern patt)方法用于检查下一个完整令牌是否满足给定模式。

  • hasNext(String patt) method is used to check whether the next complete token meets the pattern (patt) formed from the given string.

    hasNext(String patt)方法用于检查下一个完整令牌是否满足从给定字符串形成的模式(patt)。

  • These methods may throw an exception at the time of checking the given pattern.

    这些方法在检查给定模式时可能会引发异常。

    IllegalStateException: This exception may throw when this Scanner is not opened.

    IllegalStateException :如果未打开此扫描器,则可能引发此异常。

  • These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,可通过类对象访问;如果尝试使用类名称访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, hasNext(),

    在第一种情况下, hasNext()

    • It does not accept any parameter.
  • In the second case, hasNext(Pattern patt),

    在第二种情况下, hasNext(Pattern patt)

    • Pattern patt – represents the pattern to read.
    • 模式patt –表示要读取的模式。
  • In the second case, hasNext(String patt),

    在第二种情况下, hasNext(String patt)

    • String patt – represents this string to denotes the pattern to read.
    • 字符串patt –表示此字符串,表示要读取的模式。

Return value:

返回值:

In all the cases, the return type of the method is boolean,

在所有情况下,方法的返回类型均为boolean 。

  • In the first case, it returns true when this Scanner has any other token in its input exists otherwise it returns false.

    在第一种情况下,当此扫描程序的输入中存在任何其他令牌时,它返回true ,否则返回false

  • In the second and third case, it returns true when this Scanner has any other token exists meet the given pattern (patt).

    在第二种和第三种情况下,当此扫描器具有满足给定模式( patt )的任何其他令牌时,它将返回true

Example:

例:

// Java program is to demonstrate the example
// of hasNext() method of Scanner

import java.util.*;
import java.util.regex.*;

public class HasNext {
 public static void main(String[] args) {
  String str = "Java Programming! 3 * 8= 24";

  // Instantiates Scanner
  Scanner sc = new Scanner(str);

  // By using hasNext() method is to
  // check whether this object has more
  // token or not
  boolean status = sc.hasNext();
  System.out.println("sc.hasNext(): " + status);

  // By using hasNext(Pattern) method is to
  // check whether the given pattern exists
  // in this object or not
  status = sc.hasNext(Pattern.compile("...a"));
  System.out.println("sc.hasNext(Pattern.compile(...a)): " + status);

  // By using hasNext(String) method is to
  // check whether the given pattern exists
  // formed from the given string or not
  status = sc.hasNext("..0.a");
  System.out.println("sc.hasNext(..0.a): " + status);

  // Scanner closed
  sc.close();
 }
}

Output

输出量

sc.hasNext(): true
sc.hasNext(Pattern.compile(...a)): true
sc.hasNext(..0.a): false


翻译自: https://www.includehelp.com/java/scanner-hasnext-method-with-example.aspx

java hasnext