• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Question on Apache Commons CSV Parser

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, wondering if anyone could help with have a question about this parser API doc

My code has TWO for-each loops which should both iterate through the CSVRecord(s) contained within the CSV Parser object. However, it will only execute the FIRST of the for-each loops (regardless of which I put first). I don't know why ??

 
Saloon Keeper
Posts: 26020
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because, I imagine, that the iterator that's created on the first for-each is actually operating by doing a line-by-line read of the input file. Creating a second iterator doesn't rewind the file (not all input source can be rewound, in fact). so you get an instant end-of-data and an apparent "no-op".

If you want to parse over the same data multiple times, have it decant the input file into memory. See the API section titled "Parsing into memory". That will construct a List which may be iterated as many times as you like - even in parallel, if you like.
 
John Mulholland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim. I'll try your suggestion.

Regards John
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic