Results 1 to 8 of 8
Thread: String Tokenizer help
- 04-17-2011, 09:41 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
String Tokenizer help
Hello, I'm having some trouble with a program I am trying to make - This program is supposed to go through a phrase, or sentence entered by the user, and replace any four letter word with four asterisks (e.g four gets replaced with ****)
I need this to be pretty basic - What I have so far is a while phrase.hasMoreTokens loop.. Is it possible to individually select each token and replace it with the asterisks? I was thinking something along this lines of.. "If this token has 4 characters then replace with 4 asterisks". I am a bit confused as how I can do this.. Or if there is a better way. Thanks
- 04-17-2011, 10:19 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Is it possible to individually select each token and replace it with the asterisks?
What StringTokenizer gives you is a method, nextToken(), which returns small strings (one per token) one at a time.
The tokens "as a whole" don't exist anywhere so there's no real question of "replacing" one token with another. Rather you get the tokens one at a time and you have to do something with each and every one of them. For instance if you want to end up with another string which is like the first but sanitised you might do something like:
Java Code:create an output string while there are more tokens append a space to the output get the next token if it has length 4 append "****" to the output else append the token itself to the output
A StringBuilder is often used to append (concatenate) strings.
------------------
The StringTokenizer docs linked to above state "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", although it has to be admitted that using regexes requires some work.
There is also BreakIterator which locates words in text taking locale and other linguistic considerations into account (hyphenation, punctuation etc).
- 04-17-2011, 11:11 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
That is kinda what I have been trying to do, but how can I get the length of the individual tokens, and what do you mean by "append a space to the output"
Edit: Heres what I have so far.. Im getting a NoSuchElement exception..
This is what I'm trying to get it to do..
While there are more tokens in the phrase, it should then go to the first token, count the amount of characters(which is index), if this token has an index of 3, then make it ****, and if not, output it as it is? I am still a bit confused heh.
Java Code:StringTokenizer phrase = new StringTokenizer (input, " ", true); while (phrase.hasMoreTokens ()) { for (index = 0 ; index < input.length () ; index++) { currentChar = input.charAt (index); currentStr = String.valueOf (currentChar); test = phrase.nextToken (); if (index == 3) { test = "****"; c.print (test); } else { test = test; } } }Last edited by Arigo; 04-17-2011 at 11:38 PM.
- 04-18-2011, 12:31 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You don't need the nested for loop. In the while loop extract the next token and then test it. You will need a string or stringbuilder declared before the loop so you can concatenate the items as you go through the loop.
- 04-18-2011, 12:43 AM #5
- 04-18-2011, 01:02 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Thanks for not directly giving me the answer. My code ended up looking like this.
Which seems to be working okay. Although probably not as efficient as it could be.Java Code:{ int index = 0; String word; String censor = ""; StringTokenizer phrase = new StringTokenizer (input, " ", true); while (phrase.hasMoreTokens ()) { word = phrase.nextToken (); if (word.length() == 4) { word = "****"; c.print (word); } else { c.print(word); } }
- 04-18-2011, 01:44 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
I'm glad you've got it doing what you want.
Just to explain the business about appending a space: you have used the StringTokenizer constructor that tells it to return the delimiter ie the space. In this case you just print the space (because its length is 1). Had you used the some other constructor which does not return the space as if it were a word you would have to insert a space. Only in that case would you have to say something like
Java Code:else { c.print(" "); c.print(word); }
- 04-18-2011, 02:03 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
String Tokenizer
By udit ajmera in forum Java SoftwareReplies: 0Last Post: 03-05-2011, 06:35 PM -
Need help with string tokenizer
By ShortIt in forum New To JavaReplies: 1Last Post: 02-18-2011, 07:04 PM -
String Tokenizer help
By GreenTea in forum New To JavaReplies: 4Last Post: 10-30-2010, 02:44 AM -
String Tokenizer
By viperlasson in forum New To JavaReplies: 1Last Post: 03-09-2010, 01:14 PM -
string tokenizer
By twinytwo in forum New To JavaReplies: 2Last Post: 03-26-2009, 02:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks