Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava.io.StreamTokenizer Class in Java | Set 2

Java.io.StreamTokenizer Class in Java | Set 2

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

    This article is contributed by Mohit Gupta 🙂. If you like Lazyroar and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments