Scanner useLocale() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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)



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.