java.util.Scanner locale()

Description :

This java tutorial shows how to use the locale() method of Scanner class of java.util package. This method returns a Locale which the Scanner class is using.

Method Syntax :

public Pattern delimiter()

Parameter Input :

 

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

 

Method Returns :

This method simply returns this Scanner’s locale.

Compatibility Version :

Requires Java 1.5 and up

Exception :

None

Discussion :

The Scanner locale() method is helpful in determining the Locale being used by the scanner object. This is very important factor in dealing with primitive and during matching of regular expressions. Locale affects the Scanner object behaves.

Java Code Example :

This java example source code demonstrates the use of locale() method of Scanner class. Basically this sample prints locale specific information which the Scanner object has been using.

package com.teknoscope.java.tutorial.scanner;

import java.util.Locale;
import java.util.Scanner;

/*
 * This is a java example source code that shows how to use locale()
 * method of Scanner class. This sample code just prints the Locale being used
 * by the Scanner object
 */

public class ScannerLocaleDemo {

	public static void main(String[] args) {

		// Initialize Scanner object
		Scanner scan = new Scanner("Grace Salvador/Female/21");

		// Get the Locale that has been set on our Scanner
		Locale locale = scan.locale();
		// Display useful locale information
		System.out.println("Locale:"+locale);
		System.out.println("Display country:"+locale.getDisplayCountry());
		System.out.println("Diplay name:"+locale.getDisplayName());

		// closing the Scanner object
		scan.close();

	}

}

Sample Output :

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

Locale:en_US
Display country:United States
Diplay name:English (United States)

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :