The toString() method of java.util.Scanner class returns the string representation of this Scanner. The exact format is unspecified.
Syntax:
public String toString()
Return Value: This function returns the string representation of this scanner.
Below program illustrates the above function:
Program 1:
// Java program to illustrate the // toString() method of Scanner class in Java import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { String s = "GeeksForGeeks - " + "A Computer Science Portal for Geeks"; // 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()); // display information about this scanner System.out.println("\nString representation " + "of this Scanner:\n" + scanner.toString()); // close the scanner scanner.close(); } } |
Scanner String:
GeeksForGeeks - A Computer Science Portal for Geeks
String representation of this Scanner:
java.util.Scanner[delimiters=\p{javaWhitespace}+]
[position=51][match valid=true][need input=false]
[skipped=false][group separator=\,]
[decimal separator=\.][positive prefix=]
[negative prefix=\Q-\E][positive suffix=]
[negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#toString()
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Scanner useRadix() method in Java with Examples
- Scanner hasNextFloat() method in Java with Examples
- Scanner nextLong() method in Java with Examples
- Scanner skip() method in Java with Examples
- Scanner nextBoolean() method in Java with Examples
- Scanner hasNextBoolean() method in Java with Examples
- Scanner hasNextDouble() method in Java with Examples
- Scanner useLocale() method in Java with Examples
- Scanner nextInt() method in Java with Examples
- Scanner hasNextLine() method in Java with Examples
- Scanner nextBigInteger() method in Java with Examples
- Scanner findInLine() method in Java with Examples
- Scanner nextShort() method in Java with Examples
- Scanner radix() method in Java with Examples
- Scanner hasNextInt() method in Java with Examples
- Scanner useDelimiter() method in Java with Examples
- Scanner close() method in Java with Examples
- Scanner nextByte() method in Java with Examples
- Scanner delimiter() method in Java with Examples
- Scanner reset() 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.

