How to parse this text format

S

Sharp

Hi

Consider a text file containing text formatted as follows:

// input.txt
1234 aaaaaaaaaaaaaaaaaaaa
1232 bbbbbbbbbbbbbbbb
3454 cccccccccccccccccc
3214 dddddddddddddddd

1234 aaaaaaaaaaaaaaaaaaaa
1232 bbbbbbbbbbbbbbbb
3454 cccccccccccccccccc
3214 dddddddddddddddd


Expect the following result when parsed:

// result.txt
1234 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1232 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
3454 cccccccccccccccccccccccccccccccccccc
3214 dddddddddddddddddddddddddddddddd


As you can see each row of text is wrapped into 2 rows, but in reality there
are many wraps.

My proposed approach:

1. create 4 string buffers
2. read each line using filereader/buffered reader
3. append the data into the corresponding string buffers

Is there a better way of doing this?

Cheers
Sharp
 
T

TechBookReport

Sharp said:
Hi

Consider a text file containing text formatted as follows:

// input.txt
1234 aaaaaaaaaaaaaaaaaaaa
1232 bbbbbbbbbbbbbbbb
3454 cccccccccccccccccc
3214 dddddddddddddddd

1234 aaaaaaaaaaaaaaaaaaaa
1232 bbbbbbbbbbbbbbbb
3454 cccccccccccccccccc
3214 dddddddddddddddd


Expect the following result when parsed:

// result.txt
1234 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1232 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
3454 cccccccccccccccccccccccccccccccccccc
3214 dddddddddddddddddddddddddddddddd


As you can see each row of text is wrapped into 2 rows, but in reality there
are many wraps.

My proposed approach:

1. create 4 string buffers
2. read each line using filereader/buffered reader
3. append the data into the corresponding string buffers

Is there a better way of doing this?

Cheers
Sharp
What happens when the number of unique rows goes beyond four? You're
hard coding information that makes this a maintenance headache. If the
keys (1234, 1232 etc) are unique then use a HashMap. If the key already
exists in the map then append the line else add it to the map.

HTH
=====================================================================
TechBookReport Java http://www.techbookreport.com/JavaIndex.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top