Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StreamTokenizer help

 
fred fredricks
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, does any1 know any good tutorial or examples of StreamTokenizer?? or maybe some1 can help me out a little?
i need to count words from a txt file..
i am really stupid and dont understand what it says in my txt book!
thanks!!
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mike,
here is a sample code i wrote...

the input file i used is,]
stktest.txt
-----------
12313
243525 100 324200 2342 maulin
ew222342fw
sdfsfsa2342
output
------
12313.0
243525.0
100.0
324200.0
2342.0
Ignoring WORD:maulin:
Ignoring WORD:ew22:
2342.0
Ignoring WORD:fw:
Ignoring WORD:sdfsfsa:
2342.0

i would try to explain the flow of events here...
- once we created the StreamTokenizerTest object and provided a file name to the argument of the method tokenizeFile() we are in the tokenizeFile() method...
- on Line-1 we create a Input stream to read the file
- on Line-2 we create a StreamTokenizer object that can tokenize the input file by reading it.
to do this we passed Reader type (which is FileReader) object to its constructor
- Line-3 says that -"run in the loop reading the file in tokens separated by white spaces untill you hit End Of File EOF". The StreamTokenizer.TT_EOF is a variable to indicate EOF value when the tokenizer reaches EOF while reading the file...
- Line-4 says that if the current token is "Number" then process it. "ttype" is a field in StreamToeknizer object to obtain the token type read by the tokenizer. the variable "nval" in the StreamToeknizer is to obtain the "number" value if the token was "number" indicated by "ttype".
- Line-5 is similary for WORD tokens (which are not numbers) and Line-6 is to identify the "new line" tokens in the file..
on Line-7 we close the FileReader as StreamTokenizer has reached the EOF and no more reading of the file is needed...
for more look at,
1. applet at this location
2. More explanation about the StreamTokenizer
3. Yet another tutorial
i bet u can find thousands on google...
hope this helps..
regards
maulin.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
the second link i meant was,
this not the one i gave in above post...
this is better..
regards
maulin.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic