• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to parse a complex file  RSS feed

 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I need to parse a file which would look something like -

1^This is a sample header
2^John^R^Doe
3^100 Main Street^Somecity^Somestate^11111^000-000-0000
4^Like Boating
4^Like Horse Riding
2^Jane^W^Doe
3^100 Some Street^Somecity^Somestate^00000^111-111-1111
4^Like Knitting
4^Like Skydiving
4^Reading
2^William^^Doe
3^1 White Street^Somewhere^Somestate^22222^333-333-3333
2^Bill^T^Doe
3^200 Some Street^Somewhere^Somestate^33333^444-444-4444
4^Like Sports
4^Like Fishing

The number beginning of each line is grouping certain type of data information. Like '1' is for the header, '2' for full name, 4 for hobbies (There could be 0 or multiple occurences of number 4).

Wondering what would be the best way to get the related data based on the numbers loaded into different formatted file which would somewhat look like -
This is a sample header
John|R|Doe100 Main Street|Somecity|Somestate|11111|000-000-0000|Like Boating
John|R|Doe100 Main Street|Somecity|Somestate|11111|000-000-0000|Like Horse Riding

...

Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks pretty straightforward... Here is a possible (not optimal) solution in pseudo code -- because I can't tell if this is a homework problem and it can be tedious once you add error checking.



Henry
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry! I'll try out what you've suggested.
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also consider String.replaceAll ():
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Santa Maria:
You might also consider String.replaceAll ():
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html


In this example, the output lines are actually formed from combinations of lines from the input.

How would replaceAll() work here?

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!