Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading words in a phrase without Tokenizer

 
greg burgess
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I'm looking for somehelp to select words from a text file and then compare the size of each word(vector class I guess) But the key issue is that I cannot!!! use a StringTokenize.
I've managed to put the file (now a String) into an array count the number of spaces (as case) and vowels but to pick out each word what should I do. I previously worked out how to pick out the first and last words in a short phrase using a while loop of while(loopIndex >=0&&line.charAt(loopIndex) !=' '). What can I change to pick out each word in turn???
Yours Greg
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg
this sounds like a homework assignment so I'll give you a push in the right direction...
I assume your defining a word as anything surrounded by whitespace?
your steps would be this:
read in a line into a String
while you haven't reached the end of the String
find the index of the next whitespace
everything between that index and the previous one is a word
do what ever you need to do with the word
done looping
you need a couple,of index variables to keep track of the current and previous index of whitespace characters. Some of the String methods to look at are:
indexOf()
length()
substring()
hope that helps


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic