Java Scanner hasNext() 方法



描述

java Scanner hasNext() 方法返回 true,如果此掃描器在其輸入中還有另一個標記。此方法可能會阻塞,同時等待輸入進行掃描。掃描器不會越過任何輸入。

宣告

以下是 java.util.Scanner.hasNext() 方法的宣告

public boolean hasNext()

引數

返回值

當且僅當此掃描器有另一個標記時,此方法返回 true

異常

IllegalStateException − 如果此掃描器已關閉

Java Scanner hasNext(Pattern pattern) 方法

描述

java.util.Scanner.hasNext(Pattern pattern) 方法返回 true,如果下一個完整標記與指定的模式匹配。完整標記以匹配分隔符模式的輸入為字首和字尾。此方法可能會阻塞,同時等待輸入。掃描器不會越過任何輸入。

宣告

以下是 java.util.Scanner.hasNext() 方法的宣告

public boolean hasNext(Pattern pattern)

引數

pattern − 要掃描的模式

返回值

當且僅當此掃描器有另一個與指定模式匹配的標記時,此方法返回 true

異常

IllegalStateException − 如果此掃描器已關閉

Java Scanner hasNext(String pattern) 方法

描述

java.util.Scanner.hasNext(String pattern) 方法返回 true,如果下一個標記與從指定字串構造的模式匹配。掃描器不會越過任何輸入。形式為 hasNext(pattern) 的此方法呼叫與呼叫 hasNext(Pattern.compile(pattern)) 的行為完全相同。

宣告

以下是 java.util.Scanner.hasNext() 方法的宣告

public boolean hasNext(String pattern)

引數

pattern − 指定要掃描的模式的字串

返回值

當且僅當此掃描器有另一個與指定模式匹配的標記時,此方法返回 true

異常

IllegalStateException − 如果此掃描器已關閉

使用字串上的掃描器檢查下一個標記是否可用示例

以下示例演示了 Java Scanner hasNext() 方法的使用,以檢查下一個標記是否可用。我們使用給定的字串建立了一個掃描器物件。然後,我們使用 hasNext() 方法檢查了標記,然後我們使用 nextLine() 方法列印了字串。現在,使用 hasNext() 檢查是否還有更多標記,然後使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // check if the scanner has a token
      System.out.println("" + scanner.hasNext());

      // print the rest of the string
      System.out.println("" + scanner.nextLine());

      // check if the scanner has a token after printing the line
      System.out.println("" + scanner.hasNext());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

true
Hello World! 3 + 3.0 = 6
false

使用模式示例檢查下一個標記是否可用

以下示例演示了 Java Scanner hasNext(Pattern pattern) 方法的使用,以檢查下一個與給定模式匹配的標記是否可用。我們使用給定的字串建立了一個掃描器物件。然後,我們使用 hasNext(pattern) 方法檢查了標記,然後我們使用 nextLine() 方法列印了字串。現在,使用 hasNext(pattern) 檢查是否還有更多標記,然後使用 close() 方法關閉掃描器。

package com.tutorialspoint;

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

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // check if the scanner's next token matches "rld" following 2 chars
      System.out.println("" + scanner.hasNext(Pattern.compile("..rld")));

      // check if the scanner's next token matches "llo" following 2 chars
      System.out.println("" + scanner.hasNext(Pattern.compile("..llo")));

      // print the rest of the string
      System.out.println("" + scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

false
true
Hello World! 3 + 3.0 = 6

使用字串作為模式的字串上的掃描器示例檢查下一個標記是否可用

以下示例演示了 Java Scanner hasNext(String pattern) 方法的使用,以檢查下一個與給定模式匹配的標記是否可用。我們使用給定的字串建立了一個掃描器物件。然後,我們使用 hasNext(pattern) 方法檢查了標記,然後我們使用 nextLine() 方法列印了字串。現在,使用 hasNext(pattern) 檢查是否還有更多標記,然後使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6"; 

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // check if the scanner's next token matches "World"
      System.out.println("" + scanner.hasNext("World"));

      // check if the scanner's next token matches "Hello"
      System.out.println("" + scanner.hasNext("Hello"));

      // print the rest of the string
      System.out.println("" + scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

false
true
Hello World! 3 + 3.0 = 6
java_util_scanner.htm
廣告