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.


    public String toString();


  • It does not accept any parameter.

Return value:

The return type of the method is String, it returns string representation of this Scanner.


// 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.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]


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.