Open In App

Scanner useLocale() method in Java with Examples

Last Updated : 11 Oct, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The useLocale() method of java.util.Scanner class sets this scanner’s locale to the specified locale.

Syntax:

public Scanner useLocale(Locale locale)

Parameters: The function accepts a mandatory parameter locale which specifies a string specifying the locale to use.

Return Value: The function returns this modified scanner.

Exceptions: If the radix is less than Character.MIN_RADIX or greater than Character.MAX_RADIX, then an IllegalArgumentException is thrown.

Below programs illustrate the above function:

Program 1:




// Java program to illustrate the
// Scanner useLocale() method in Java
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        String s = "Geeksforgeeks has Scanner Class Methods";
  
        // create a new scanner
        // with the specified String Object
        Scanner scanner = new Scanner(s);
  
        // print a line of the scanner
        System.out.println("Scanner String: \n"
                           + scanner.nextLine());
  
        // display the previous locale
        System.out.println("Current Lcoale: "
                           + scanner.locale());
  
        // change the locale of the scanner
        scanner.useLocale(Locale.ENGLISH);
        System.out.println("Changing Locale to ENGLISH");
  
        // display the new locale
        System.out.println("Updated Locale: "
                           + scanner.locale());
  
        // close the scanner
        scanner.close();
    }
}


Output:

Scanner String: 
Geeksforgeeks has Scanner Class Methods
Current Lcoale: en_US
Changing Locale to ENGLISH
Updated Locale: en

Program 2:




// Java program to illustrate the
// Scanner useLocale() method in Java
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        String s = "Geeksforgeeks 2018";
  
        // create a new scanner
        // with the specified String Object
        Scanner scanner = new Scanner(s);
  
        // print a line of the scanner
        System.out.println("Scanner String: \n"
                           + scanner.nextLine());
  
        // display the previous locale
        System.out.println("Current Lcoale: "
                           + scanner.locale());
  
        // change the locale of the scanner
        scanner.useLocale(Locale.FRENCH);
        System.out.println("Changing Locale to FRENCH");
  
        // display the new locale
        System.out.println("Updated Locale: "
                           + scanner.locale());
  
        // close the scanner
        scanner.close();
    }
}


Output:

Scanner String: 
Geeksforgeeks 2018
Current Lcoale: en_US
Changing Locale to FRENCH
Updated Locale: fr

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useLocale(java.util.Locale)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads