Java Scanner useLocale()方法及示例
java.util.Scanner 类的 useLocale() 方法将该扫描仪的locale设置为指定的locale。
语法
public Scanner useLocale(Locale locale)
参数: 该函数接受一个强制性参数 locale ,它指定了一个字符串,指明要使用的locale。
返回值: 该函数返回这个 修改后的扫描仪
异常: 如果radix小于Character.MIN_RADIX或大于Character.MAX_RADIX,那么将抛出IllegalArgumentException。
下面的程序说明了上述函数。
程序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
程序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
**参考资料: ** https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useLocale(java.util.Locale)