當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。