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:
// 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(); } } |
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(); } } |
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)
Recommended Posts:
- Scanner nextDouble() method in Java with Examples
- Scanner nextShort() method in Java with Examples
- Scanner nextBigInteger() method in Java with Examples
- Scanner nextByte() method in Java with Examples
- Scanner nextLong() method in Java with Examples
- Scanner nextInt() method in Java with Examples
- Scanner radix() method in Java with Examples
- Scanner toString() method in Java with Examples
- Scanner useDelimiter() method in Java with Examples
- Scanner nextFloat() method in Java with Examples
- Scanner reset() method in Java with Examples
- Scanner nextBoolean() method in Java with Examples
- Scanner hasNextBigDecimal() method in Java with Examples
- Scanner hasNextBigInteger() method in Java with Examples
- Scanner hasNextInt() method in Java with Examples
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.



