Java Scanner locale() 方法



描述

java Scanner locale() 方法返回此掃描器的區域設定。

宣告

以下是 java.util.Scanner.locale() 方法的宣告

public Locale locale()

引數

返回值

此方法返回此掃描器的區域設定

異常

獲取字串上掃描器的區域設定示例

以下示例演示瞭如何使用 Java Scanner locale() 方法檢查此掃描器的區域設定。我們使用給定的字串建立了一個掃描器物件。然後我們使用 nextLine() 方法列印字串,然後列印區域設定。使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // print the next line of the string
      System.out.println(scanner.nextLine());
      
      System.out.println(scanner.locale());
      // close the scanner
      scanner.close();

   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World! 3 + 3.0 = 6
en_IN

獲取使用者輸入上掃描器的區域設定示例

以下示例演示瞭如何使用 Java Scanner locale() 方法檢查此掃描器的區域設定。我們使用 System.in 類建立了一個掃描器物件。然後我們使用 nextLine() 方法列印字串,然後列印其區域設定。然後使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      // create a new scanner with the system input
      Scanner scanner = new Scanner(System.in);

      // print the next line of the string
      System.out.println(scanner.nextLine());

      System.out.println(scanner.locale());
      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:(我們在其中輸入 Hello World 並按回車鍵。)

Hello World
Hello World
en_IN

獲取屬性檔案上掃描器的區域設定示例

以下示例演示瞭如何使用 Java Scanner locale() 方法檢查此掃描器的區域設定。我們使用檔案 properties.txt 建立了一個掃描器物件。然後我們使用 nextLine() 方法列印內容,然後列印其區域設定。然後使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) throws FileNotFoundException {

      // create a new scanner with a file as input
      Scanner scanner = new Scanner(new File("properties.txt"));

      // print the next line of the string
      System.out.println(scanner.nextLine());

      System.out.println(scanner.locale());
      // close the scanner
      scanner.close();
   }
}

假設我們在你的 CLASSPATH 中有一個名為 properties.txt 的檔案,其內容如下。此檔案將用作我們示例程式的輸入:

Height=200
Width=15

輸出

讓我們編譯並執行上述程式,這將產生以下結果:(我們在其中輸入 Hello World 並按回車鍵。)

Height=200
en_IN
java_util_scanner.htm
廣告
© . All rights reserved.