• Post Reply Bookmark Topic Watch Topic
  • New Topic

StreamTokenizer and wordChars method  RSS feed

 
simon fletcher
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a basic program that uses StreamTokenizer to 'take in' a simple math question (the operator of which could be words, like 'divided by', or a symbol, like + or -), and give the answer. I have had success with the program except for one obstacle which I just can't seem to get over.

I know there are other ways to input data, and I will start using them in the near future, but I want to finish tinkering around with StreamTokenizer first.

Basically, in order to make the tokenizer 'read' a sign (+, -, *, or /), I have to employ the wordChar method, with two int values as arguements representing a range of characters that will get treated as words instead of basically being ignored in the case of my program. I made StreamTokenizer object called tokenizer then employed the wordChars method like this tokenizer.wordChars('*','/'); which, as far as I can tell, is supposed to cover a range of characters that encompass my four math signs. Everything in the program works except it will not recognize the minus sign (-) character.

I am just wondering if anyone reading this might be able to point me in the direction of enlightnment as to why the StreamTokenizer won't recognize this character.

Thankyou.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simon fletcher wrote:Everything in the program works except it will not recognize the minus sign (-) character.
I am just wondering if anyone reading this might be able to point me in the direction of enlightnment as to why the StreamTokenizer won't recognize this character.

Well, I've never used the class (and it looks pretty arcane), but I suspect that it may regard it as a minus-sign (ie, part of a number).
How do you fix it? Dunno, but I notice that there's an ordinaryChar() method.

BTW, the docs also say that, by default, '/' is a comment character; but it's possible that your wordChars() call overrides this.

HIH

Winston
 
simon fletcher
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.

I did originally try using the ordinaryChar method but it didn't change the situation at all.

I have another way around it which basically uses the process of elimination. If, after all input has been assessed there is no mathematical operator, the question must have been a minus b. That works.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!