Java Scanner reset()方法及示例

Java Scanner reset()方法及示例

java.util.Scanner 类的 reset() 方法重置了这个扫描仪。在重置扫描器时,它 将丢弃 所有的显式状态信息,这些信息可能已经被调用 useDelimiter(java.util.regex.Pattern)、useLocale(java.util.Locale)或useRadix(int) 所改变。

语法

public Scanner reset()

返回值: 该函数返回已经复位的这个 扫描仪

下面的程序说明了上述功能。

程序1 :

// 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

程序2

// 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://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#reset()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程