• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Removing the longest words  RSS feed

 
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!
 
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.
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!