Scanner reset() method in Java with Examples
The reset() method of java.util.Scanner class resets this scanner. On resetting a scanner, it discards all of its explicit state information which may have been changed by invocations of useDelimiter(java.util.regex.Pattern), useLocale(java.util.Locale), or useRadix(int).
Syntax:
public Scanner reset()
Return Value: This function returns this scanner which has been reset.
Below programs illustrate the above function:
Program 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
Program 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
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#reset()
Recommended Posts:
- Scanner hasNextLong() method in Java with Examples
- Scanner hasNextShort() method in Java with Examples
- Scanner locale() method in Java with Examples
- Scanner nextShort() method in Java with Examples
- Scanner useRadix() method in Java with Examples
- Scanner hasNextInt() method in Java with Examples
- Scanner skip() method in Java with Examples
- Scanner hasNextByte() method in Java with Examples
- Scanner ioException() method in Java with Examples
- Scanner useLocale() method in Java with Examples
- Scanner toString() method in Java with Examples
- Scanner nextBigDecimal() method in Java with Examples
- Scanner useDelimiter() method in Java with Examples
- Scanner nextLine() method in Java with Examples
- Scanner radix() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


