Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StreamTokenizer

 
vinodh venkataraman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm using StreamTokenizer to read contents from a text file. I want to retain the spaces in the text file, but streamtokenizer removes the spaces and appends all the characters in the file. I tried using ordinaryChar() method, but that didn't really help either. Any suggestions would really help...

Reader read = new BufferedReader(new InputStreamReader(myFile));//read the contents using buffered reader
StreamTokenizer in = new StreamTokenizer(read); //read the tokens
in.ordinaryChar(' ');


Thanks.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think StreamTokenizer returns the delimiters (i.e. the spaces) to you. Why do you need them anyways? If you can explain your reasoning here, we might be able to help you find an alternate solution. Perhaps StringTokenizer will help, but I'm unsure that this is what you really need.

Layne
 
Norm Radder
Ranch Hand
Posts: 882
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StreamTokenizer can be tailored:

stzr.ordinaryChar(' '); // Return spaces

will return the space character which you handle as below in the switch stmt:
case ' ':
 
vinodh venkataraman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My program has to choose random sentences based on a randomly generated line number from a text file. I thought using StreamTokenizer will help me use the lineno() function for selecting a random line number.
Anyways, I'm gonna try the case ' ' option and see how it works...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic