Home »
Java programming language
Java Scanner toString() Method with Example
Scanner Class toString() method: Here, we are going to learn about the toString() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on February 18, 2020
Scanner Class toString() method
- toString() method is available in java.util package.
- toString() method is used to string denotation of this Scanner and it contains information related to tracing.
- toString() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- toString() method does not throw an exception at the time of string denotation of this Scanner.
Syntax:
public String toString();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is String, it returns string representation of this Scanner.
Example:
// Java program to demonstrate the example
// of String toString() method of Scanner
import java.util.*;
public class ToStringOfScanner {
public static void main(String[] args) {
String str = "Hi, true \n IncludeHelp! 8 + 2.0f = 10.0f";
// Instantiate Scanner with the
// given str
Scanner sc = new Scanner(str);
// Display Scanner
System.out.println("sc.nextLine(): " + sc.nextLine());
// By using toString() method is to
// represent string for this Scanner
System.out.println("sc.toString(): " + sc.toString());
// close the scanner
sc.close();
}
}
Output
sc.nextLine(): Hi, true
sc.toString(): java.util.Scanner[delimiters=\p{javaWhitespace}+][position=10][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]