java.util.Scanner useLocale(Locale locale)

Description :

This java tutorial shows how to use the useLocale(Locale locale) method of Scanner class of java.util package. This method sets the Locale to be used by this Scanner object overriding the default locale.

Method Syntax :

public Scanner useLocale(Locale locale)

Parameter Input :

 

DataType Parameter Description
Locale locale The locale to be used by this Scanner

 

Method Returns :

This method simply returns this Scanner object with Locale being set as method parameter.

Compatibility Version :

Requires Java 1.5 and up

Exception :

None

Discussion :

The Scanner useLocale() method is helpful to set a locale specific setting which affects many elements in primitive matching regular expressions.

Java Code Example :

This java example source code demonstrates the use of useLocale(Locale locale) method of Scanner class. Basically we just set the Locale setting to our scanner class. We have used the static constant Locale.English to set the method argument Locale.

package com.javatutorialhq.java.tutorial.scanner;

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

/*
 * This is a java example source code that shows how to use useLocale(Locale locale)
 * method of Scanner class. We utilized the static variables of Locale class
 * to set the locale specific setting to Scanner class
 */

public class ScannerUseLocaleDemo {

	public static void main(String[] args) {

		// Initialize Scanner object
		Scanner scan = new Scanner("William Hectre/Male/24");
		// initialize the string delimiter
		scan.useDelimiter("/");
		// Printing the delimiter used
		System.out.println("The delimiter used is "+scan.delimiter());
		// set the locale to be used
		Locale locale = Locale.ENGLISH;
		// set the locale to the scanner object
		scan.useLocale(locale);
		// Printing the tokenized Strings
		while(scan.hasNext()){
			System.out.println(scan.next());
		}
		// closing the scanner stream
		scan.close();

	}

}

Sample Output :

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

java scanner useLocale(locale) method example

java scanner useLocale(locale) method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :