Java.io.StreamTokenizer Class in Java | Set 2
Last Updated :
09 Jan, 2017
Improve
StringTokenizer Class in Java | Set 1
Methods:

- 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 // 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; } } } }
Word : Hello Word : Geeks Number : 1.0 Word : This Number : 2.0 Number : 3.0 Word : is Word : about Number : 4.0 Word : parseNumbers
- 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 // 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."); } } } }
Word : Hell o encountered. Word : Geeks Word : This Word : is Word : ab o encountered. Word : qu o encountered.
- 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 // 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."); } } } }
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.
- 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 // 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; } } } }
Word : This Word : program Word : is Word : about Word : slashSlashComments
- 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 // 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; } } } }
Word : This Word : program Word : is Word : about Word : slashStarComments Number : 123.0
- 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 // 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; } } } }
Word : This Word : progr Word : m Word : is Word : out Word : whitesp Word : eCh Word : rs