hi all, for a class project we need to write our own string tokenizer class, i have managed to get the bones of it working but am having difficulty tokenizing the last word of a string. heres what i've got code: so you can see i'm checking for spaces and getting the words that way, but of course there is no space at the end of a string so any ideas as to how i might get that last word into my array. i realize i could just insert a space at the end of the string but that'd be cheating. is the an ascii character that indicates end of text, well there is now that i think of it but can i use it here. thanks for any suggestions you may have. billo [ edited to preserve formatting using the [code] and [/code] UBB tags -ds ] [ April 12, 2003: Message edited by: Dirk Schreckmann ]
Please use *code* tags when posting code. You can just cheat and assume that you are already working on a word when you hit the end of your loop. So as soon as you get out of the loop put the next "word" into your token array.
Please ignore post, I have no idea what I am talking about.
The easiest answer is after you have completed your loop, check to see if word has any length. If it does, add it to your array.
And just as a side note, you might consider using a StringBuffer to build your words. It is more efficient to use the append(XXX) methods than to reassign the word through += type constructs. [ April 11, 2003: Message edited by: Joel McNary ]
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
posted 16 years ago
thats great i'll try that if(word.hasmoreTokens) trick. unfortunately we are limited to using the + operator for this project, but i will definitely be using string buffers in the future. ta much p.s apologies if the code snippet wasn't formatted correctly, won't happen again.