当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Scanner useLocale()用法及代码示例


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)



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Scanner useLocale() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。