Java.util.Scanner.useDelimiter() Method


Advertisements


Description

The java.util.Scanner.useDelimiter(Pattern pattern) method sets this scanner's delimiting pattern to the specified pattern.

Declaration

Following is the declaration for java.util.Scanner.useDelimiter() method

public Scanner useDelimiter(Pattern pattern)

Parameters

  • pattern -- A delimiting pattern

Return Value

This method returns this scanner

Exception

  • NA

Example

The following example shows the usage of java.util.Scanner.useDelimiter() method.

package com.tutorialspoint;

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

public class ScannerDemo {

   public static void main(String[] args) {

   String s = "Hello World! 3 + 3.0 = 6.0 true ";

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

   // print a line of the scanner
   System.out.println("" + scanner.nextLine());

   // change the delimiter of this scanner
   scanner.useDelimiter(Pattern.compile(".ll."));

   // display the new delimiter
   System.out.println("" + scanner.delimiter());

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

Let us compile and run the above program, this will produce the following result:

Hello World! 3 + 3.0 = 6.0 true 
.ll.

java_util_scanner.htm

Advertisements