Java Utility Library

Java Scanner - toString() Method



The java.util.Scanner.toString() method returns the string representation of the scanner. The string representation of a scanner contains information that may be useful for debugging. The exact format is unspecified.

Syntax

public String toString()

Parameters

No parameter is required.

Return Value

Returns string representation of the scanner

Exception

NA.

Example:

In the example below, the java.util.Scanner.toString() method returns the string representation of the given scanner.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {

    //String to scan
    String MyString = "Hello World 10 + 20 = 30.0";

    //creating a Scanner
    Scanner MyScan = new Scanner(MyString);

    //print a line from the Scanner
    System.out.println(MyScan.nextLine());

    //print the string representation of the Scanner
    System.out.println(MyScan.toString()); 

    //close the Scanner
    MyScan.close();      
  }
}

The output of the above code will be:

Hello World 10 + 20 = 30.0
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=26][match valid=true][need input=false][source closed=true][skipped=false][group separator=\x{2c}][decimal separator=\x{2e}][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q?\E]

❮ Java.util - Scanner