Java Utility Library

Java Scanner - useLocale() Method



The java.util.Scanner.useLocale() method is used to set the scanner's locale to the specified locale. A scanner's locale affects many elements of its default primitive matching regular expressions. Invoking the reset() method will set the scanner's locale to the initial locale.

Syntax

public Scanner useLocale(Locale locale)

Parameters

locale Specify locale - a string specifying the locale to use.

Return Value

Returns the scanner.

Exception

NA.

Example:

In the example below, the java.util.Scanner.useLocale() method is used to set the scanner's locale to the specified locale.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {

    //String to scan
    String MyString = "Hello World 10 + 20 = 30.0";

    //creating a Scanner
    Scanner MyScan = new Scanner(MyString);

    //set the scanner's locale to GERMAN
    Locale locale = new Locale("GERMANY", "GERMAN");
    MyScan.useLocale(locale);

    //print the new locale
    System.out.println(MyScan.locale());

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

The output of the above code will be:

germany_GERMAN

❮ Java.util - Scanner