Java中的扫描器 useLocale() 方法及示例
Java.util.Scanner类的useLocale()方法将此扫描仪的语言环境设置为指定的语言环境。
句法:
public Scanner useLocale(Locale locale)
参数:该函数接受一个强制参数locale ,该参数指定一个字符串,指定要使用的语言环境。
返回值:函数返回这个修改后的扫描仪。
例外:如果基数小于字符.MIN_RADIX或大于字符.MAX_RADIX ,则抛出IllegalArgumentException 。
下面的程序说明了上述函数:
方案一:
// 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
方案二:
// 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
参考: https: Java Java.util.Locale)