Skip to content
Related Articles
Open in App
Not now

Related Articles

Scanner toString() method in Java with Examples

Improve Article
Save Article
  • Last Updated : 11 Oct, 2018
Improve Article
Save Article

The toString() method of java.util.Scanner class returns the string representation of this Scanner. The exact format is unspecified.


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 String: 
GeeksForGeeks - A Computer Science Portal for Geeks

String representation of this Scanner:
[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]


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!