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()
相關用法
- Java Scanner hasNextShort()用法及代碼示例
- Java Scanner nextFloat()用法及代碼示例
- Java Scanner findInLine()用法及代碼示例
- Java Scanner nextBigInteger()用法及代碼示例
- Java Scanner close()用法及代碼示例
- Java Scanner delimiter()用法及代碼示例
- Java Scanner nextByte()用法及代碼示例
- Java Scanner hasNextBigInteger()用法及代碼示例
- Java Scanner locale()用法及代碼示例
- Java Scanner nextLine()用法及代碼示例
- Java Scanner nextBigDecimal()用法及代碼示例
- Java Scanner nextBoolean()用法及代碼示例
- Java Scanner nextDouble()用法及代碼示例
- Java Scanner ioException()用法及代碼示例
- Java Scanner nextLong()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Scanner reset() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。