It's not a secret anymore!
The moose likes Beginning Java and the fly likes StringTokenizer confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer confusion" Watch "StringTokenizer confusion" New topic

StringTokenizer confusion

Sally Curtis

Joined: Jan 10, 2004
Posts: 26
hi, i am writing a quadratics program. I want to use the string tokenizer to separate the 'problem' into three sections. for ex. -3x^2 + 2x -1....i want to have -3x^2 +2x and -1. How would i use each of the tokens, individualy? Can i do that?
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
It seems to me that this problem is a little more difficult than it might first appear. Two issues:
(1) You want to "keep" the thing you're splitting with - you want to split at + or -, but the + or - is significant and is kept as part of the string to its right.
(2) The language you're describing could be made quite complex, depending on how much effort you want to put in.
I'm not an expert on parsing/language structure, but I think you'll want to make as many assumptions about the formation of the structure of the input strings as possible. (Eg, how many terms are you allowed? Can you have just 'x + 1'? Is the unknown variable always 'x'? Can you have multiple terms of the same order (eg 2x^2 + x + 2x + 1)? Is whitespace allowed? Do the highest-power terms always come first?
I think you'll have more luck with the split() method of String and regular expressions. If you're unfamiliar with regular expressions this could be a little difficult however.
That said, someone else out there could have a much more simple solution.

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You can try something like this:
new StringTokenizer(myString, "+-", true):
This would yield 6 tokens:
You could combine strings to get what you asked.
To solve the problem with more help from Java, take a look at StreamTokenizer. It will break up the expression completely into labels and operators, treat multi-character numbers and labels as single tokens, tell you what type each token is, etc. On the other hands, your HW may require using StringTokenizer, and StreamTokenizer is more complicated to learn.

Mike Gershman
SCJP 1.4, SCWCD in process
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
I might consider splitting the whole thing into an array of characters and working from there. But then again I don't know how to use regular expressions so that might be the better way

SCJP 1.4,
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I don't think you can even use a stream tokenizer because it only yields at TT_EOF, TT_EOL, TT_NUMBER, and TT_WORD. This would not include the exponent that you are trying to include. The best way to solve this equation is to use a stack, but I didn't think you just wanted to solve it. Lemme work on this a little more and see what I can come up with on the stream tokenizer.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
A StreamTokenizer will return special characters, which it calls "ordinary characters", as separate tokens whose ttype is their character value. This will work for ^
To be sure + and - are treated this way and not part of a number, include these calls:
I agree. Here's the link:
subject: StringTokenizer confusion
jQuery in Action, 3rd edition