java.util.Scanner类的useLocale()方法将此扫描程序的语言环境设置为指定的语言环境。
用法:
public Scanner useLocale(Locale locale)
参数:该函数接受一个强制性参数语言环境,该语言环境指定一个字符串,该字符串指定要使用的语言环境。
返回值:该函数返回修改后的扫描仪。
异常:如果基数小于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)
相关用法
- Java Scanner nextByte()用法及代码示例
- Java Scanner nextInt()用法及代码示例
- Java Scanner hasNextInt()用法及代码示例
- Java Scanner hasNextByte()用法及代码示例
- Java Scanner hasNextBigInteger()用法及代码示例
- Java Scanner hasNextDouble()用法及代码示例
- Java Scanner hasNextBoolean()用法及代码示例
- Java Scanner hasNextBigDecimal()用法及代码示例
- Java Scanner nextBigInteger()用法及代码示例
- Java Scanner locale()用法及代码示例
- Java Scanner nextLine()用法及代码示例
- Java Scanner ioException()用法及代码示例
- Java Scanner nextBigDecimal()用法及代码示例
- Java Scanner nextBoolean()用法及代码示例
- Java Scanner hasNextShort()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Scanner useLocale() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。