• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer question

 
Brandt Charles
Ranch Hand
Posts: 57
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringTokenizer is deprecated and should not be used anymore. Use String.split() or regex package instead.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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...
 
Jar Jaquiso
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of manipulation do you need to do?
 
Brandt Charles
Ranch Hand
Posts: 57
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic