File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes StringTokenizer question 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 "StringTokenizer question" Watch "StringTokenizer question" New topic
Author

StringTokenizer question

Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

A very silly question: Can StringTokenizer be used to parse non-numeric strings? I ask because in my Intro to Java text it seems to be used only in examples where the user enters multiple numbers in an input dialog box.

I am working on an exercise in which String input is to be manipulated and had been thinking of utilizing StringTokenizer, but wasn't sure if it would work.

Thanks,

Brandt


Green, green, green!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
Absolutely, any string is possible. Strange, most of the examples I've ever seen used word lists and sentences, not numbers ... must be a new generation of introductory material out there


Ping & DNS - my free Android networking tools app
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
StringTokenizer is deprecated and should not be used anymore. Use String.split() or regex package instead.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
"Deprecated" has a specific meaning which does not apply here. StringTokenizer has not been deprecated. Though I would agree that String.split() is better, more flexible. Unfortunately it does have some other complications if the user has not learned about regular expressions yet...


"I'm not back." - Bill Harding, Twister
Jar Jaquiso
Greenhorn

Joined: Apr 20, 2006
Posts: 26
Why do you think this class is deprecated?
It doesn't say so here StringTokenizer (JavaDoc)
, nor here
Deprecated List (JavaDoc)
[ April 22, 2006: Message edited by: Jar Jaquiso ]
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Ok, you are right. Still it is discouraged from use. split is far cleaner. There is no reason to use it anyway.

"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."
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

Thanks for the input so far. I followed the link to split() in the Java Documentation, and it appears to break up a string and returns an array.

These questions are revolving around an assignment in class dealing with String manipulation. I am NOT asking for someone to tell me how to do this, but the general questions I have will help me rule out how I want to complete it.

If I use split(), in order to change the specific elements of the array, wouldn't I have to convert the String data to something that can be changed since Strings are immutable? Basically I have to move the first letter of a word to the end of that word. I was thinking of using StringBuffer after forming the array. Am I correct in my assumption that items of the String class cannot be changed, but StringBuffer can?

Again, gentle hints are appreciated, I am not fishing for actual code. The split() method was not even mentioned in our introductory text, so I am trying to discern its use without reader friendly explanation, which I expect happens in the field quite frequently.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Yes, you are right that the contents of a String can't be changed. However, you can move a reference from a String to another String.

So you could manipulate the contents of your String, and then reassign the String reference to point to the output.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
What type of manipulation do you need to do?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

Originally posted by Garrett Rowe:
What type of manipulation do you need to do?


We're taking a string of input and converting it to Pig Latin. For anyone not familiar with this, in a nutshell you take the first letter of a word, move it to the end, then add an "ay" to it. So, the word "bike" would turn into "ikebay", for example.

I've been scouring the Java docs about String and StringBuffer, I'm assuming I cannot use split() with StringBuffer since it's not the same class. That would make things much easier if I could. However, I'm getting the feeling the solution isn't as difficult as I think.

And thanks for the suggestion, Keith.
[ April 24, 2006: Message edited by: Brandt Charles ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
While you can't change String you can make a new one. Maybe the calling method says:

And your translator method could say:

StringBuffer is more efficient than appending strings if you're doing War and Peace or some millions of words. For grins you could make the output variable a StringBuffer and read up on append().

Does that answer the right question?
[ April 24, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I'm assuming I cannot use split() with StringBuffer since it's not the same class. That would make things much easier if I could...

Thats true, but you can use a String as the argument in the construction of a String buffer:

Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

Garrett and Stan, thank you for your input. Garrett, your suggestion is what I had been trying to put together coherently. Please tell me that as I learn programming that it gets easier to translate ideas into code. I appreciate the gentle hints instead of the answer itself. I haven't been in a classroom in 10 years and I think expectations of myself are set too high such that I get very frustrated at the least bit of adversity. The frustration tends to cloud my thinking.
[ April 24, 2006: Message edited by: Brandt Charles ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
I thought StringTokenizer was deprecated; it was last time i tried, so I usually use String.split().
But I looked a minute ago, and it isn't deprecated; they must have taken the deprecation off and now describe StringTokenizer as a legacy class, not recommended for new code.

CR
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Campbell Ritchie:
I thought StringTokenizer was deprecated; it was last time i tried, so I usually use String.split().
But I looked a minute ago, and it isn't deprecated; they must have taken the deprecation off and now describe StringTokenizer as a legacy class, not recommended for new code.

CR


StringTokenizer has been obsoleted, not deprecated.
The same can be said for java.util.Vector, java.util.Hashtable, java.lang.StringBuffer and a few others.

Obsoleting a type permits the existing codebase of the API spec. implementation to compile without deprecation warnings (though it will not compile without unchecked cast warnings since generics contain some inherent flaws that are unavoidable).


Tony Morris
Java Q&A (FAQ, Trivia)
 
 
subject: StringTokenizer question
 
Similar Threads
retrieving value from a text box
Alternative to StringTokenizer
how to read input from a text file
Alternative to StringTokenizer
Alternative to StringTokenizer