Java Scanner reset() 方法



描述

Java Scanner reset() 方法重置此掃描器。重置掃描器會丟棄其所有可能已被 useDelimiter(java.util.regex.Pattern)、useLocale(java.util.Locale) 或 useRadix(int) 呼叫更改的顯式狀態資訊。

宣告

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

public Scanner reset()

引數

返回值

此方法返回此掃描器

異常

在字串示例上重置掃描器

以下示例演示了使用 Java Scanner reset() 方法重置掃描器的用法。我們使用給定的字串建立了一個掃描器物件。然後,我們使用 nextLine() 方法列印字串,並使用 useLocale() 方法更新其區域設定。使用 useRadix() 方法更新基數。然後使用 reset() 方法重置掃描器,並列印值以檢查 reset() 方法的影響。然後使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;
import java.util.Locale;

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

      String s = "Hello World! 3 + 3.0 = 6.0 true ";

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

      // print a line of the scanner
      System.out.println("" + scanner.nextLine());

      // change the locale of this scanner
      scanner.useLocale(Locale.US);

      // change the radix of this scanner
      scanner.useRadix(30);

      // reset and check the values for radix and locale, which are the default
      scanner.reset();
      System.out.println(scanner.radix());
      System.out.println(scanner.locale());

      // close the scanner
      scanner.close();
   }
}

輸出

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

Hello World! 3 + 3.0 = 6.0 true 
10
en_IN

在使用者輸入示例上重置掃描器

以下示例演示了使用 Java Scanner reset() 方法重置掃描器的用法。我們使用 System.in 建立了一個掃描器物件。然後,我們使用 nextLine() 方法列印字串,並使用 useLocale() 方法更新其區域設定。使用 useRadix() 方法更新基數。然後使用 reset() 方法重置掃描器,並列印值以檢查 reset() 方法的影響。然後使用 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 the System Input
      Scanner scanner = new Scanner(System.in);

      // print a line of the scanner
      System.out.println("" + scanner.nextLine());

      // change the locale of this scanner
      scanner.useLocale(Locale.US);

      // change the radix of this scanner
      scanner.useRadix(30);

      // reset and check the values for radix and locale, which are the default
      scanner.reset();
      System.out.println(scanner.radix());
      System.out.println(scanner.locale());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:(我們輸入了 Hello World。)

Hello World
Hello World
10
en_IN

在屬性檔案示例上重置掃描器

以下示例演示了使用 Java Scanner reset() 方法重置掃描器的用法。我們使用 properties.txt 檔案建立了一個掃描器物件。然後,我們使用 nextLine() 方法列印字串,並使用 useLocale() 方法更新其區域設定。使用 useRadix() 方法更新基數。然後使用 reset() 方法重置掃描器,並列印值以檢查 reset() 方法的影響。然後使用 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 a line of the scanner
      System.out.println("" + scanner.nextLine());

      // change the locale of this scanner
      scanner.useLocale(Locale.US);

      // change the radix of this scanner
      scanner.useRadix(30);

      // reset and check the values for radix and locale, which are the default
      scanner.reset();
      System.out.println(scanner.radix());
      System.out.println(scanner.locale());

      // close the scanner
      scanner.close();
   }
}

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

Hello World! 3 + 3.0 = 6

輸出

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

Hello World! 3 + 3.0 = 6
10
en_IN
java_util_scanner.htm
廣告
© . All rights reserved.