This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer confusion" Watch "StringTokenizer confusion" New topic
Author

StringTokenizer confusion

Sally Curtis
Greenhorn

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
Hi,
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.
Cheers,

--Tim
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:
-
3x^2
+
2x
-
1
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, www.gsi3d.org.uk
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:
st.ordinaryChar('-');
st.ordinaryChar('+');
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer confusion
 
Similar Threads
Regex Woes
Multi-d array
Problem with method to evaluate e^-(x^2)
OCAJP OCPJP Java 7 Mock Certification Exam Question: Use parentheses to override operator precedence
reg expn