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.