Java Utility Library

Java Scanner - locale() Method



The java.util.Scanner.locale() method returns the scanner's locale. A scanner's locale affects many elements of its default primitive matching regular expressions.

Syntax

public Locale locale()

Parameters

No parameter is required.

Return Value

Returns the scanner's locale.

Exception

NA.

Example:

In the example below, the java.util.Scanner.locale() method is used to find the scanner's 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);

    //print a line from the Scanner
    System.out.println(MyScan.nextLine());

    //print the scanner's locale
    System.out.println(MyScan.locale()); 

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

The output of the above code will be:

Hello World 10 + 20 = 30.0
en_US

❮ Java.util - Scanner