Java - StreamTokenizer commentChar(int ch) method



Description

The Java StreamTokenizer commentChar(int ch) method specifies that the character argument starts a single-line comment. All characters from the comment character to the end of the line are ignored by this stream tokenizer. Any other attribute settings for the specified character are cleared.

Declaration

Following is the declaration for java.io.StreamTokenizer.commentChar(int ch) method.

public void commentChar(int ch)

Parameters

ch − The character.

Return Value

This method does not return a value.

Exception

NA

Example - Usage of StreamTokenizer commentChar(int ch) method

The following example shows the usage of StreamTokenizer commentChar(int ch) method.

StreamTokenizerDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.StreamTokenizer;

public class StreamTokenizerDemo {
   public static void main(String[] args) {
      String text = "Hello. This is a text that will be split into tokens. " 
         + " 1 + 1 = 2";
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeUTF(text);
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // create a new tokenizer
         Reader r = new BufferedReader(new InputStreamReader(ois));
         StreamTokenizer st = new StreamTokenizer(r);

         // set character "a" as a commentChar
         st.commentChar('a');

         // print the stream tokens
         boolean eof = false;
         
         do {
            int token = st.nextToken();
            
            switch (token) {
               case StreamTokenizer.TT_EOF:
                  System.out.println("End of File encountered.");
                  eof = true;
                  break;
                  
               case StreamTokenizer.TT_EOL:
                  System.out.println("End of Line encountered.");
                  break;
                  
               case StreamTokenizer.TT_WORD:
                  System.out.println("Word: " + st.sval);
                  break;
                  
               case StreamTokenizer.TT_NUMBER:
                  System.out.println("Number: " + st.nval);
                  break;
                  
               default:
                  System.out.println((char) token + " encountered.");
                  
                  if (token == '!') {
                     eof = true;
                  }
            }
         } while (!eof);

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

@ encountered.
Word: Hello.
Word: This
Word: is
End of File encountered.

Example - Use # as a comment character

The following example shows the usage of StreamTokenizer commentChar(int ch) method.

StreamTokenizerDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.StringReader;

public class StreamTokenizerDemo {
   public static void main(String[] args) throws IOException {
      String input = "Java 101 # This is a comment\nPython 202";

      Reader reader = new StringReader(input);
      StreamTokenizer tokenizer = new StreamTokenizer(reader);

      tokenizer.commentChar('#'); // Treat '#' as start of comment

      System.out.println("Tokens:");
      while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
         if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
            System.out.println("Word: " + tokenizer.sval);
         } else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
            System.out.println("Number: " + tokenizer.nval);
         }
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Tokens:
Word: Java
Number: 101.0
Word: Python
Number: 202.0

Explanation

  • Input includes # This is a comment, which gets skipped.

  • Only Java 101 and Python 202 are parsed as tokens.

Example - Use ! as a comment character

The following example shows the usage of StreamTokenizer commentChar(int ch) method.

StreamTokenizerDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.StringReader;

public class StreamTokenizerDemo {
   public static void main(String[] args) throws IOException {
      String input = "C++ 303 ! C-style comment\nGo 404";

      Reader reader = new StringReader(input);
      StreamTokenizer tokenizer = new StreamTokenizer(reader);

      tokenizer.commentChar('!'); // Treat '!' as comment

      System.out.println("Tokens:");
      while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
         if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
            System.out.println("Word: " + tokenizer.sval);
         } else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
            System.out.println("Number: " + tokenizer.nval);
         }
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Tokens:
Word: C
Number: 303.0
Word: Go
Number: 404.0

Explanation

  • Everything after ! on the first line is ignored.

  • Only C++ 303 and Go 404 are processed.

java_io_streamtokenizer.htm