📜  Java中的扫描仪 reset() 方法及示例

📅  最后修改于: 2022-05-13 01:54:23.360000             🧑  作者: Mango

Java中的扫描仪 reset() 方法及示例

Java.util.Scanner类的reset()方法重置此扫描仪。在重置扫描器时,它会丢弃所有可能已通过调用useDelimiter(Java.util.regex.Pattern)、useLocale(Java.util.Locale) 或 useRadix(int) 更改的显式状态信息。

句法:

public Scanner reset()

返回值:此函数返回已重置的扫描仪

下面的程序说明了上述函数:

方案一:

// Java program to illustrate the
// reset() method of Scanner class 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());
  
        // change the locale of this scanner
        scanner.useLocale(Locale.US);
  
        // change the radix of this scanner
        scanner.useRadix(30);
  
        System.out.println("\nBefore Reset:\n");
  
        // print the values before reset
        System.out.println("Radix: " + scanner.radix());
        System.out.println("Locale: " + scanner.locale());
  
        // reset
        scanner.reset();
  
        System.out.println("\nAfter Reset:\n");
  
        System.out.println("Radix: " + scanner.radix());
        System.out.println("Locale: " + scanner.locale());
  
        // close the scanner
        scanner.close();
    }
}
输出:
Scanner String:
Geeksforgeeks has Scanner Class Methods

Before Reset:

Radix: 30
Locale: en_US

After Reset:

Radix: 10
Locale: en_US

方案二:

// Java program to illustrate the
// reset() method of Scanner class in Java
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        String s = "Geeksforgeeks";
  
        // 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());
  
        // change the locale of this scanner
        scanner.useLocale(Locale.US);
  
        // change the radix of this scanner
        scanner.useRadix(12);
  
        System.out.println("\nBefore Reset:\n");
  
        // print the values before reset
        System.out.println("Radix: " + scanner.radix());
        System.out.println("Locale: " + scanner.locale());
  
        // reset
        scanner.reset();
  
        System.out.println("\nAfter Reset:\n");
  
        System.out.println("Radix: " + scanner.radix());
        System.out.println("Locale: " + scanner.locale());
  
        // close the scanner
        scanner.close();
    }
}
输出:
Scanner String:
Geeksforgeeks

Before Reset:

Radix: 12
Locale: en_US

After Reset:

Radix: 10
Locale: en_US

参考: https: Java/util/Scanner.html#reset()