java.util.Scanner delimiter()

Description :

This java tutorial shows how to use the delimiter() method of Scanner class of java.util package. This method returns a Pattern which the Scanner class is using as a delimiter. This method is simply just to get the delimiter being used by the Scanner class.

Method Syntax :

public Pattern delimiter()

Parameter Input :

 

DataType Parameter Description
N/A N/A N/A

 

Method Returns :

This method simply returns the Pattern which the Scanner class is using.

Compatibility Version :

Requires Java 1.5 and up

Exception :

None

Discussion :

The Scanner delimiter() method is helpful to determine what has been used as delimiter in evaluating the string which has been read as declared on the class constructor.

This method is very helpful in error tracing especially on cases that the Scanner object failed to tokenize.

Java Code Example :

This java example source code demonstrates the use of delimiter() method of Scanner class. Basically it prints the delimiter that is being used by the Scanner object scan which is the default pattern since we didn’t specify delimiter to be used. The code then tokenize the String declared on the constructor of the Scanner object.

package com.teknoscope.java.tutorial.scanner;

import java.util.Scanner;

/*
 * This is an example source code that prints the delimiter
 * and print the string tokens
 */

public class ScannerDelimiterDemo {

	public static void main(String[] args) {

		// Initialize Scanner object
		Scanner scan = new Scanner("This a sample string");
		// Printing the delimiter used
		System.out.println("Delimiter:"+scan.delimiter());
		// Printing the tokenized Strings
		while(scan.hasNext()){
			System.out.println(scan.next());
		}
		// closing the scanner stream
		scan.close();

	}

}

Sample Output :

Running the delimiter() method example source code of Scanner class will give you the following output

java scanner delimiter() method example

java scanner delimiter() method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :