This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

help needed creating MyStringTokenizer  RSS feed

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
billo bailey
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

p.s apologies if the code snippet wasn't formatted correctly, won't happen again.


This guy we like.
 
That feels good. Thanks. Here's a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!