• Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing the longest words  RSS feed

 
Keitsi Lindberg
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text file that contains names:
"Joe Jack Albert
Lisa Jim
Elizabeth Tina Tom"
I can read the file, but how can I remove the longest name from each line and save the file (without the longest names) with a new name.I think I should use StringTokenizer but how?
Thank's for your help!
 
Jeremy Thornton
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is to use an InputStream capable of reading a line at a time and then feed each line into a StringTokenizer. then read the tokens from the Tokeniser into a collection, maybe sort by length and write out all but the longest string.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a basic approach. Read in each line and use a StringTokenizer to break it into individual words, saving the words in a list (e.g., ArrayList). Then loop through the list searching for the longest word. Remove the longest word from the list and write out the remaining ones in the output file. Continue until all lines have been processed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!