• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing the longest words

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic