nextToken(): This method is available in the java.io.StreamTokenizer class of Java.
Syntax:
int java.io.StreamTokenizer.nextToken() throws IOException
This method parses the next token from the input stream of this tokenizer. The type of the next token is returned in the ttype field.
Additional information about the token may be in the nval field or the sval field of this tokenizer.
Parameters: NA
Returns: the value of the ttype field.
Throws:
1. IOException - if an I/O error occurs.
Approach 1: When no exception
Java
import java.io.IOException;import java.io.Reader;import java.io.StreamTokenizer;import java.io.StringReader;public class StreamTokenizernextToken {public static void main(String[] args) {Reader r = new StringReader("HELLO");StreamTokenizer streamTokenizer =new StreamTokenizer(r);try {System.out.println(streamTokenizer.nextToken());} catch (IOException e) {System.out.println("IOException occurs");}}}
Output:
-3
Approach 2: IOException
Java
import java.io.IOException;import java.io.PipedReader;import java.io.Reader;import java.io.StreamTokenizer;public class StreamTokenizernextToken {public static void main(String[] args) {Reader r = new PipedReader();StreamTokenizer streamTokenizer =new StreamTokenizer(r);try {System.out.println(streamTokenizer.nextToken());} catch (IOException e) {System.out.println("IOException occurs");}}}
Output:
IOException occurs
Some other methods of StreamTokenizer
commentChar(int): This method specified that the character argument starts a single-line comment.
eolIsSignificant(boolean): This method determines whether or not the ends of the line are treated as tokens.
lineno(): This method returns the current line number.
lowerCaseMode(boolean): This method determines whether or not word tokens are automatically lowercase.
nextToken(): This method parses the next token from the input stream of this tokenizer.
ordinaryChar(int): This method specifies that the character argument is "ordinary" in this tokenizer.
ordinaryChars(int, int): This method specifies that all characters c in the range low <= c <= high are "ordinary" in this tokenizer.
parseNumbers(): Specifies that numbers should be parsed by this tokenizer.
pushBack(): This method causes the next call to the nextToken method of this tokenizer to return the current value in the ttype field, and not modify the value in the nval or sval field.
quoteChar(int): This method specifies that matching pairs of this character delimit string constants in this tokenizer.
resetSyntax(): This method resets this tokenizer's syntax table so that all characters are "ordinary."
slashSlashComments(boolean): This method determines whether or not the tokenizer recognizes C++-style comments.
slashStarComments(boolean): This method determines whether or not the tokenizer recognizes C-style comments.
StreamTokenizer(Reader): This method creates a tokenizer that parses the given character stream.
toString(): This method returns the string representation of the current stream token and the line number it occurs on.
whitespaceChars(int, int): This method specifies that all characters c in the range low <= c <= high are white space characters.
wordChars(int, int): This method specifies that all characters c in the range low <= c <= high are word constituents.
StreamTokenizer constants: StreamTokenizer.TT_EOF,StreamTokenizer.TT_EOL,etc.
No comments:
Post a Comment