Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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.
 
Bring out your dead! Or a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!