Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java.io.Printstream Class in Java | Set 2
Next article icon

Java.io.StreamTokenizer Class in Java | Set 2

Last Updated : 09 Jan, 2017
Comments
Improve
Suggest changes
Like Article
Like
Report

StringTokenizer Class in Java | Set 1

StreamTokenizer Class - Set 2

Methods:

  1. parseNumbers() : java.io.StreamTokenizer.parseNumbers() specifies that the number in StreamTokenizer is parsed, so that each character – ” 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ” has a numeric attribute.
    When the parser encounters a word token that has the format of a double precision floating-point number, it treats the token as a number rather than a word, by setting the ttype field to the value TT_NUMBER and putting the numeric value of the token into the nval field.
    Syntax :
      public void parseNumbers()  Parameters :  -----------  Return :  void

    Implementation :




    // Java Program  illustrating use of parseNumbers() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
        FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
            // Use of parseNumbers() method
            // specifies that the number in StreamTokenizer is parsed
             token.parseNumbers();
      
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
      
                }
            }
        }
    }
     
     

    Note :
    This program won’t run here as no ‘ABC’ file exists. You can check this code on Java compiler on your system.
    To check this code, create a file ‘ABC’ on your system.
    ‘ABC’ file contains :

    Hello Geeks 1
    This 2
    3is
    about 4
    parseNumbers()

    Output :

      Word : Hello  Word : Geeks  Number : 1.0  Word : This  Number : 2.0  Number : 3.0  Word : is  Word : about  Number : 4.0  Word : parseNumbers
  2. quoteChar() : java.io.StreamTokenizer.quoteChar(int arg) specifies that it delimits the matching character as string constant in StreamTokenizer.
    When the nextToken method encounters a string constant, the ttype field is set to the string delimiter and the sval field is set to the body of the string.
    Syntax :
      public void quoteChar(int arg)  Parameters :  arg : the character to be dilimit   Return :  void

    Implementation :




    // Java Program  illustrating use of quoteChar() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
                                                 FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
            // specify o as a quote char
            token.quoteChar('o');
      
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                default:
                    System.out.println((char) t + " encountered.");
      
                }
            }
        }
    }
     
     

    Note :
    This program won’t run here as no ‘ABC’ file exists. You can check this code on Java compiler on your system.
    To check this code, create a file ‘ABC’ on your system.
    ‘ABC’ file contains :

    Hello
    Geeks
    This
    is
    about
    quoteChar()

    Output :

      Word : Hell  o encountered.  Word : Geeks  Word : This  Word : is  Word : ab  o encountered.  Word : qu  o encountered.
  3. resetSyntax() : java.io.StreamTokenizer.resetSynatx() resets Syntax when a number is met, so that all characters are set as ‘Ordinary’ in StreamTokenizer.
    Syntax :
      public void resetSyntax()  Parameters :  ---------  Return :  void

    Implementation :




    // Java Program  illustrating use of resetSyntax() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
                                                 FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
             
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
                case StreamTokenizer.TT_NUMBER:
                      
                     // Use of resetSyntax() 
                     token.resetSyntax();
      
                    System.out.println("Number : " + token.nval);
                    break;
                default:
                    System.out.println((char) t + " encountered.");
      
                }
            }
        }
    }
     
     

    Note :
    This program won’t run here as no ‘ABC’ file exists. You can check this code on Java compiler on your system.
    To check this code, create a file ‘ABC’ on your system.
    ‘ABC’ file contains :

    Hello
    This
    is
    resetSyntax()
    1 xmpl
    2 🙂
    3
    Output :

      Word : Hello  Word : This  Word : is  Word : resetSyntax  ( encountered.  ) encountered.  Number : 1.0    encountered.  x encountered.  m encountered.  p encountered.  l encountered.   encountered.     encountered.  2 encountered.    encountered.  : encountered.  ) encountered.   encountered.     encountered.  3 encountered.
  4. slashSlashComments() : java.io.StreamTokenizer.slashSlashComments(boolean arg) specifies whether to consider C++ – style comments by tokenizer or not. If ‘arg’ is set true, then the StreamTokenizer recognises and ignores C++ – style comments. ‘//’ is considered as starting of a comment.
    If the flag argument is false, then C++- style comments are not treated specially.
    Syntax :
      public void slashSlashComments(boolean arg)  Parameters :  arg : tells whether to recognise and ignore C++ - style comments or not.  Return :  void

    Implementation :




    // Java Program  illustrating use of slashSlashComments() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
                                                   FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
            // Use of slashSlashComments()
            // Here 'arg' is set to true i.e. to recognise and ignore C++style Comments
            boolean arg = true;
            token.slashSlashComments(arg);
      
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                }
            }
        }
    }
     
     

    Note :
    This program won’t run here as no ‘ABC’ file exists. You can check this code on Java compiler on your system.
    To check this code, create a file ‘ABC’ on your system.
    ‘ABC’ file contains :

    This program is about slashSlashComments // method

    This method considers ‘method’ in ABC.txt file as an comment and thus ignores it.
    Output :

      Word : This  Word : program  Word : is  Word : about  Word : slashSlashComments
  5. slashStarComments() : java.io.StreamTokenizer.slashStarComments(boolean arg) specifies whether to consider C – style comments by tokenizer or not. If ‘arg’ is set true, then the StreamTokenizer recognises and ignores C – style comments. ‘/*……*/’ is considered as a comment.
    Syntax :
      public void slashStarComments(boolean arg)  Parameters :  arg : tells whether to recognise and ignore C - style comments or not.  Return :  void

    Implementation :




    // Java Program illustrating use of slashStarComments() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
                                                  FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
            // Use of slashStarComments()
            // Here 'arg' is set to true i.e. to recognise and ignore Cstyle Comments
            boolean arg = true;
            token.slashStarComments(true);
      
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                }
            }
        }
    }
     
     

    Note :
    This program won’t run here as no ‘ABC’ file exists. You can check this code on Java compiler on your system.
    To check this code, create a file ‘ABC’ on your system.
    ‘ABC’ file contains :

    This program is about slashStarComments /* method */ 123

    This method considers ‘method’ in ABC.txt file as an comment and thus ignores it.
    Output :

      Word : This  Word : program  Word : is  Word : about  Word : slashStarComments  Number : 123.0
  6. whitespaceChars() : java.io.StreamTokenizer.whitespaceChars(int low, int high) specifies all the characters in the range of low to high as white space, which serves only to separate tokens in the InputStream.
    Syntax :
      public void whitespaceChars(int low, int high)  Parameters :  low : lower range of character to be white spaced.  high : higher range of character to be white spaced   Return :  void

    Implementation :




    // Java Program illustrating use of whitespaceChars() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
                                             FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
               
            // Use of whitespaceChars() method
            // Here range is low = 'a' to high = 'c'
            token.whitespaceChars('a','d');
      
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                }
            }
        }
    }
     
     

    Note :
    This program won’t run here as no ‘ABC’ file exists. You can check this code on Java compiler on your system.
    To check this code, create a file ‘ABC’ on your system.
    ‘ABC’ file contains :

    This program is about whitespaceChars()

    Output :

      Word : This  Word : progr  Word : m  Word : is  Word : out  Word : whitesp  Word : eCh  Word : rs


    Next Article
    Java.io.Printstream Class in Java | Set 2

    M

    Mohit Gupta
    Improve
    Article Tags :
    • Java
    • Java-I/O
    Practice Tags :
    • Java

    Similar Reads

    • Java.io.StreamTokenizer Class in Java | Set 1
      Java.io.StreamTokenizer class parses input stream into "tokens".It allows to read one token at a time. Stream Tokenizer can recognize numbers, quoted strings, and various comment styles. Declaration : public class StreamTokenizer extends Object Constructor : StreamTokenizer(Reader arg) : Creates a t
      9 min read
    • Java.io.Printstream Class in Java | Set 2
      Java.io.Printstream Class in Java | Set 1More Methods: PrintStream printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this output stream using the specified format string and arguments. Syntax :public PrintStream printf(Locale l, String format, Obje
      6 min read
    • Java.io.Printstream Class in Java | Set 1
      A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the c
      5 min read
    • Java.io.ObjectInputStream Class in Java | Set 2
      Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStrea
      6 min read
    • Java.io.OutputStream class in Java
      This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Constructo
      2 min read
    • Java.io.StringReader class in Java
      StringReader class in Java is a character stream class whose source is a string. It inherits Reader Class. Closing the StringReader is not necessary, it is because system resources like network sockets and files are not used. Let us check more points about StringReader Class in Java. Declare StringR
      4 min read
    • Java.io.StringWriter class in Java
      Java StringWriter class creates a string from the characters of the String Buffer stream. Methods of the StringWriter class in Java can also be called after closing the Stream as this will raise no IO Exception. Declaration in Java StringWriter Classpublic class StringWriter extends WriterConstructo
      6 min read
    • Java.io.PipedInputStream class in Java
      Pipes in IO provides a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination. PipedInputStream is also piped with PipedOutputStream. So, data can be written using PipedOutputStream and can be written using PipedInputStream.But, using both threads
      5 min read
    • Java.io.ObjectInputStream Class in Java | Set 1
      ObjectInputStream Class deserializes the primitive data and objects previously written by ObjectOutputStream. Both ObjectOutputStream and ObjectInputStream are used as it provides storage for graphs of object.It ensures that the object it is working for, matches the classes of JVM i.e Java Virtual M
      9 min read
    • Java.io.Writer class in Java
      This abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
      4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences