wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to use the API to read StringTokenizer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to use the API to read StringTokenizer?" Watch "How to use the API to read StringTokenizer?" New topic
Author

How to use the API to read StringTokenizer?

Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Hi all,

I was browsing through 'The Java Developers Almanac' and found this StringTokenizer API which I don't understand. I hope someone can help me, please? Thank you!



1) Where can we find the StringTokenizer class? Does it come with the JDK?
2) Why is it called 'parser'? Can we rename it to something else?
3) What is processWord, is it like a method?

Thank you for your replies.
aditee sharma
Ranch Hand

Joined: Jul 22, 2008
Posts: 182
1) Where can we find the StringTokenizer class? Does it come with the JDK?
Yes.java.util.StringTokenizer

2) Why is it called 'parser'? Can we rename it to something else?
Yes. How about "tweety" ?

3) What is processWord, is it like a method?
just a arbitrary name to illustrate that you can pass the String token obtained from StringTokenizer to any method you like.

I guess you would like to explore more in Sun docs.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18542
    
  40

1) Where can we find the StringTokenizer class? Does it come with the JDK?

http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html

2) Why is it called 'parser'? Can we rename it to something else?

From the code that you shown, it looks like a variable, so I guess you can name it to anything that you like.

3) What is processWord, is it like a method?

That is not part of the String tokenizer. It looks to be a method that has been defined somewhere else in your book.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38042
    
  22
. . . and when you did find StringTokenizer, did you find the bit telling you not to use it? It says to use String#split and the Regular Expressions API instead.

[edit]Fix broken link[/edit]
[ September 25, 2008: Message edited by: Campbell Ritchie ]
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Hi Aditee, Henry, Campbell,
Thanks for the explanations! I actually know a bit about StringTokenizer, but just wanted to confirm my understanding. Thank you!

Originally posted by Campbell Ritchie:
StringTokenizer, did you find the bit telling you not to use it? It says to use String#split and the URL=http://java.sun.com/docs/books/tutorial/essential/regex/index.html]Regular Expressions API[/URL] instead.

Campbell, I think the first link is broken. Hmm, the don't use StringTokenizer bit? I didn't see it mentioned anywhere. Did I miss it?

I haven't read the regex part yet. It sounds more complicated than the tokenizer.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18542
    
  40

Campbell, I think the first link is broken. Hmm, the don't use StringTokenizer bit? I didn't see it mentioned anywhere. Did I miss it?


The actual quote from the JavaDoc reads as....

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.


Henry
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
But take a look here.

It seems StringTokenizer is considerably faster than String.split for sufficiently large number of tokens.

What do you think guys?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38042
    
  22
Originally posted by LS chin:
Campbell, I think the first link is broken. Hmm, the don't use StringTokenizer bit? I didn't see it mentioned anywhere. Did I miss it?

I haven't read the regex part yet. It sounds more complicated than the tokenizer.


The link was broken; thank you for pointing it out. There was a duplication of the http:// bit which I have corrected. But the delimiter for StringTokenizer behaves a bit like a regular expression anyway.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Originally posted by Gamini Sirisena:
But take a look here.

It seems StringTokenizer is considerably faster than String.split for sufficiently large number of tokens.

What do you think guys?


Well, it is true, but when you start parsing strings that are so large that the difference in performance matters, then you should look into other ways of parsing them than using split or StringTokenizer.

My opinion is that if you find yourself stuck in parsing huge amounts of text, redesign it so that it uses XML or direct data source access.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to use the API to read StringTokenizer?
 
Similar Threads
parse date format
Extracting integers from strings
C++ friend functionality in JAVA
JNI problem, pasing String to native (C function) by reference
can this be called mutable?