StreamTokenizer sample program in Java
By: Emiley J in Java Tutorials on 2022-09-15
StreamTokenizer defines several methods. In this example, we will use only a few. To reset the default set of delimiters, we will employ the resetSyntax() method. The default set of delimiters is finely tuned for tokenizing Java programs and is thus too specialized for this example. We declare that our tokens, or "words," are any consecutive string of visible characters delimited on both sides by whitespace.
import java.io.*; public class StreamTokenizerDemo { public static void main(String[] args) throws IOException { String input = "42 true 3.14 \"hello\" end"; Reader reader = new StringReader(input); StreamTokenizer tokenizer = new StreamTokenizer(reader); // Set up tokenizer properties tokenizer.quoteChar('"'); tokenizer.ordinaryChar(' '); // Read and print tokens while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) { switch (tokenizer.ttype) { case StreamTokenizer.TT_NUMBER: System.out.println("Number: " + tokenizer.nval); break; case StreamTokenizer.TT_WORD: System.out.println("Word: " + tokenizer.sval); break; case '"': System.out.println("Quoted string: " + tokenizer.sval); break; default: System.out.println("Other character: " + (char) tokenizer.ttype); break; } } } }
In this example, we create a StreamTokenizer
instance to parse the string "42 true 3.14 "hello" end". We then set some properties on the tokenizer, such as using double quotes as the quote character and treating space as an ordinary character.
We then read tokens from the tokenizer using the nextToken()
method, which returns a token type constant indicating the type of the next token, such as TT_NUMBER
for a numeric value or TT_WORD
for a string value. We use a switch statement to handle each token type, printing out the token value or character as appropriate.
Here are some of the important methods provided by StreamTokenizer
:
nextToken()
: Returns the type of the next token in the input stream.sval
: Returns the string value of the current token if it is a word or quoted string.nval
: Returns the numeric value of the current token if it is a number.ttype
: Returns the type of the current token as an integer constant.quoteChar(char c)
: Sets the quote character to use for parsing quoted strings.ordinaryChar(char c)
: Treats the specified character as an ordinary character rather than a delimiter.eolIsSignificant(boolean flag)
: Indicates whether end-of-line characters should be treated as separate tokens.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Comments