Scanner useLocale() method in Java with Examples
Last Updated :
11 Oct, 2018
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:
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "Geeksforgeeks has Scanner Class Methods" ;
Scanner scanner = new Scanner(s);
System.out.println( "Scanner String: \n"
+ scanner.nextLine());
System.out.println( "Current Lcoale: "
+ scanner.locale());
scanner.useLocale(Locale.ENGLISH);
System.out.println( "Changing Locale to ENGLISH" );
System.out.println( "Updated Locale: "
+ scanner.locale());
scanner.close();
}
}
|
Output:
Scanner String:
Geeksforgeeks has Scanner Class Methods
Current Lcoale: en_US
Changing Locale to ENGLISH
Updated Locale: en
Program 2:
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "Geeksforgeeks 2018" ;
Scanner scanner = new Scanner(s);
System.out.println( "Scanner String: \n"
+ scanner.nextLine());
System.out.println( "Current Lcoale: "
+ scanner.locale());
scanner.useLocale(Locale.FRENCH);
System.out.println( "Changing Locale to FRENCH" );
System.out.println( "Updated Locale: "
+ scanner.locale());
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)
Share your thoughts in the comments
Please Login to comment...