- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包补充
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
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