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.This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in Java )
Step by Step guide to setup freetts for Java
Open a .docx file and show content in a TextArea using Java
concurrent.Flow instead of Observable class in Java
DateFormat sample program in Java
Simple Port Scanner application using Java
Using the AWS SDK for Java in Eclipse
Read a file having a list of telnet commands and execute them one by one using Java
Calculator application in Java
Latest Articles (in Java)
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)
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)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate