Java Scanner next() 方法



描述

Java Scanner next() 方法查詢並返回此掃描器的下一個完整標記。完整標記的前後都有與分隔符模式匹配的輸入。即使先前呼叫 hasNext() 返回 true,此方法也可能在等待輸入進行掃描時阻塞。

宣告

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

public String next()

引數

返回值

此方法返回下一個標記

異常

  • NoSuchElementException − 如果沒有更多標記可用

  • IllegalStateException − 如果此掃描器已關閉

Java Scanner next(Pattern pattern) 方法

描述

java Scanner next(Pattern pattern) 方法返回下一個與指定模式匹配的標記。即使先前呼叫 hasNext(Pattern) 返回 true,此方法也可能在等待輸入進行掃描時阻塞。如果匹配成功,掃描器將跳過與模式匹配的輸入。

宣告

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

public String next(Pattern pattern)

引數

pattern − 要掃描的模式

返回值

此方法返回下一個標記

異常

  • NoSuchElementException − 如果沒有更多標記可用

  • IllegalStateException − 如果此掃描器已關閉

Java Scanner next(String pattern) 方法

描述

java Scanner next(String pattern) 方法返回下一個與從指定字串構造的模式匹配的標記。如果匹配成功,掃描器將跳過與模式匹配的輸入。

宣告

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

public String next(String pattern)

引數

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

返回值

此方法返回下一個標記

異常

  • NoSuchElementException − 如果沒有更多標記可用

  • IllegalStateException − 如果此掃描器已關閉

字串上掃描器的下一個標記示例

以下示例演示瞭如何使用 Java Scanner next() 方法獲取下一個標記。我們使用給定字串建立了一個掃描器物件。然後我們使用 next() 方法列印標記,再次使用 next() 方法列印標記,然後使用 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);

      // find the next token and print it
      System.out.println(scanner.next());

      // find the next token and print it
      System.out.println(scanner.next());

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

輸出

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

Hello
World!

匹配模式的字串上掃描器的下一個標記示例

以下示例演示瞭如何使用 Java Scanner next(Pattern pattern) 方法獲取與給定模式匹配的下一個標記。我們使用給定字串建立了一個掃描器物件。然後我們使用 next(pattern) 方法列印標記,再次使用 next(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 next token matches the pattern and print it
      System.out.println(scanner.next(Pattern.compile("..llo")));

      // check if next token matches the pattern and print it
      System.out.println(scanner.next(Pattern.compile("..rld!")));

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

輸出

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

Hello
World!

匹配字串作為模式的字串上掃描器的下一個標記示例

以下示例演示瞭如何使用 Java Scanner next(String pattern) 方法獲取與給定模式匹配的下一個標記。我們使用給定字串建立了一個掃描器物件。然後我們使用 next(pattern) 方法列印標記,再次使用 next(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 next token matches the pattern and print it
      System.out.println(scanner.next("Hello"));

      // check if next token matches the pattern and print it
      System.out.println(scanner.next("World!"));

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

輸出

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

Hello
World!
java_util_scanner.htm
廣告