Results 1 to 4 of 4
Thread: Which to use?
- 12-29-2010, 04:55 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Which to use?
If you have a sentence, and you separate it into tokens (which would be the words of the sentence in this case), how would you work with the tokens? Would you work with the original string variable?
Here's what I mean, where this part of code is part of a method. I want to replace every 'a' in the words with an 'e':
Java Code:String sentence="insart taxt hara"; StringTokenizer words = new StringTokenizer (sentence); StringBuffer speech = new StringBuffer (); String phrase=""; while (words.hasMoreTokens()) { for (int x = 0; x < sentence.length(); x++) { if (sentence.charAt(x) == 'a') { sentence = sentence.replace('a', 'e'); } } } speech.append(words.nextToken()); phrase = speech.toString(); System.out.println(phrase);
- 12-29-2010, 05:27 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Java Code:while (words.hasMoreTokens()) { //... } speech.append(words.nextToken());
Doesn't this result in some sort of runtime exception?
---------
It's not really clear what you are trying to do here. If you want to replace all the 'a' characters with 'e' just use replace() once. This has nothing to do with tokenising.
- 12-29-2010, 05:39 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
I know that I could do that, but I want to separate the sentence into words and replace the a's separately, word by word.
My compiler's being a pain, I have to check about the error.
- 12-30-2010, 04:58 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks