aspose file tools*
The moose likes Beginning Java and the fly likes could someone explain stringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "could someone explain stringTokenizer" Watch "could someone explain stringTokenizer" New topic
Author

could someone explain stringTokenizer

tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
I am trying to use stringTokenizer. However, my books don't give any examples of how to use it. What I'm trying to do is to take a string like "1234, 5678" and have it split it at the comma. So, from my current understanding, it would be like:

After this point, I get lost. What I'm trying to do is set each set of numbers to a string and then use the Integer.parseInt method to set them all to integers. Can someone give me some help on how to do this? Thanks.
Jesse Thompson
Greenhorn

Joined: Nov 17, 2000
Posts: 10
This is the first time I tried to use this class, and I got it to work. Here is the code i used:

The API specification gives a pretty good simple example. http://java.sun.com/products/jdk/1.2/docs/api/index.html

[This message has been edited by Jesse Thompson (edited December 14, 2000).]
tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
I've changed around my code a bit to pull in rgb values from a parameter tag and part of it seems to be working. However I keep getting this error message:
missing return statement
private Color rgbReturn(String str) {
and it points to the {. These braces are really getting me screwed up. From my code, it looks like I have them all, but I guess there's something that I'm missing. Does anyone see a problem here?

Obviously, my applet isn't done yet. I'm just starting with the code
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Try this:

The error you were getting was because your return is inside of a conditional statement. Because a while loop tests its condition before executing, it is possible that the body of the loop never executes. The compiler won't let you put your only return statement in there if it thinks it might not be reached.
Also, if you are going to use the hasMoreTokens() method to control your loop, it is probably not a good idea to make more than one call to nextToken() in the body of the loop. It creates a situation where you will end up with an exception. If the string has one more token and you enter the loop, then you call nextToken three times, your last two calls will generate the runtime exception NoSuchElementException.
[This message has been edited by Bodie Minster (edited December 14, 2000).]
tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
I think I follow most of what you're suggesting here, but there is one area where I still struggle to understand the concept. It's where you put:

especially the i<=tokens.countTokens()+1 portion of the for loop. Could you explain this in just a little more detail? Thank you very much for your help thus far.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sorry, my bad. That line worked but was a bit misleading. Instead of doing:

I should have done

The problem with the first method is that the value of countTokens changes each time the loop runs because we have consumed another token.
Since you know that you are expecting three tokens, you can start by declaring a String array to hold the three strings you are going to parse. The method countTokens() returns an integer representing the number of times that nextToken() can be called without generating an exception. Since our string is like this:
<PRE>
value1,value2,value3
</PRE>
When we call nextToken for the first time, we will get value1.
When we call nextToken for the second time, we will get value2.
Our third call to nextToken will give us value3. Any subsequent call to nextToken will generate a runtime exception because there are no more tokens. So our code should execute the loop 3 times because countTokens will return 3 (before we make any calls to nextToken(). After one call, countTokens() would return 2, then 1, and so on...). This way we loop until there are no more tokens and then we deal with the array. If for some reason we don't get three tokens, we return null with a message that the string wasn't valid because it didn't look like we expected it to.
Sorry about the crummy code...
[This message has been edited by Bodie Minster (edited December 15, 2000).]
[This message has been edited by Bodie Minster (edited December 15, 2000).]
[This message has been edited by Bodie Minster (edited December 19, 2000).]
tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
Okay, I'm starting to follow you a little more now, but still a bit confused. I see that you added originally a +1 after the condition. Wouldn't this make it loop through 4 times? And if so, why would you want to do that?
eric moon
Ranch Hand

Joined: Nov 26, 2000
Posts: 133
The code:
for (int i = 0; i < 3; i ++)
will excecute three times, at an i value of 0,1, and 2. I guess the incrementing doesn't occur until after the loop runs.

<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Yes, I originally had a +1 in the condition part of the FOR statement. That was the misleading part. The condition that I used didn't evaluate the same on every pass. For example, when you write a loop that says:
<PRE>
for(i=0;i<3;i++){
</PRE>
You can safely assume that 3 will always evaluate to 3. However, in my condition, I was testing i against tokens.countTokens(). This function's value changes with each pass. So the first pass i was 0 and countTokens()+1 was 4. i<=countTokens()+1 was true and the loop executed. In the body of the loop I called nextToken() and this changed the value of countTokens because I consumed one token. So on the next pass i is 1 and countTokens()+1 is now only 3. The loop still executes. On the third and final pass i is 2 and so is countTokens()+1. The loop executed three times like I wanted, but the logic was confusing. It was better to just write:
<PRE>
int tokenCount = tokens.countTokens();
for(int i=0;i<tokenCount;i++){
</PRE>
Since this tests against the value of the variable tokenCount, which we set to 3 before entering the loop and didn't change in the body of the loop, the loop will execute three times. This is a much clearer way to get the job done.
Does that make sense? I know the code was cruddy. Sorry for the confusion.
[This message has been edited by Bodie Minster (edited December 18, 2000).]
[This message has been edited by Bodie Minster (edited December 19, 2000).]
 
wood burning stoves
 
subject: could someone explain stringTokenizer