Java Scanner.match()

Syntax

Scanner.match() has the following syntax.

public MatchResult match()

Example

In the following code shows how to use Scanner.match() method.


//ww  w.  j  a  va  2  s .  co m

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {

      String s = "java2s.com 1 + 1 = 2.0 ";

      Scanner scanner = new Scanner(s);

      // check if next token is "java"
      System.out.println(scanner.hasNext("java"));

      // find the last match and print it
      System.out.println(scanner.match());

      System.out.println(scanner.nextLine());

      scanner.close();
   }
}

The code above generates the following result.