• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Parse data from a file

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

i have a file which looks something like this,
111, X, 12, 34, 56
111, Y, 12, 34, 56
122, X, 12, 34, 56
122, Y, 12, 34, 56
133, X, 12, 34, 56
133, Y, 12, 34, 56

now i have parsed this data and am storing individual records in a class and then accessing them and everything is fine.

but i need to further group this data based on the 2nd record (X and Y). and then I need to process the individual records in different ways. i mean i need to somehow group the records and then parse them. please help. thanks.
 
Pete Dawn
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried to implement the Comparator class.

and have also written a compare method. but am not sure how i would implement it.

please help. thanks.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create 2 empty lists, loop through the main list, check the 2nd field, if it is X then add it to one list, else if it is Y then add it to another list.

Pretty straightforward.

Or didn't I understood your problem?
 
Pete Dawn
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am currently saving the individual records in a class (time, date, string etc). now i should create a second class or create a new list.

not sure?
 
Pete Dawn
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also its not hard coded X and Y. i mean the 2nd field has to be same in the file. so i need to group all same 2nd fields in the file and then process them. how would i do that. thanks.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a Map<String,List><Record>> where Record is the class representing each row.

Adding is quite easy:

Afterwards, the map contains a List<Record> for each different value of the second field. You can use the map's keySet(), entrySet() and values() methods to iterate over all keys, entries and values (which are List<Record>).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic