I have a text file input which contains data as below. How can I display data from the text file into specific format?
Monday
Jessy
Walking
20 minutes
Matthew
Run
20 minutes
Karen
Jogging
40 minutes
Jessica
Run
12 minutes
Tuesday
Messia
Walking
10 minutes
Matthew
Run
20 minutes
Pete
Run
10 minutes
Carol
Walking
30 minutes
I want to display data from the text file into this format:
Day Name Type of exercise Time
Monday Jessy Walking 20 minutes
Matthew Run 20 minutes
Karen Jogging 40 minutes
Jessica Run 12 minutes
Tuesday Messia Walking 10 minutes
Matthew Run 20 minutes
Pete Run 10 minutes
Carol Walking 30 minutes
I would have a look at Java's sprintf() function and it's ability to left/right justify data with specified widths.
I just threw this together quickly, but what about something like:
And then invoke everything for example:
Regarding parsing the input:
One issue you will have is that each "record" of data (each row, in the ouput) is not a fixed size. Some are 3-tuples of name,exercise,time, and others are 4-tuples of day,name,exercise,time
That said, assuming the format you've given is really all there is to it, the issue can be worked around.
After reading a line, you could check for a weekday, and if so assume that's the start of a 4-tuple, and read the next 3 lines. If it is not a weekday, then assume it is a 3-tuple, and only read the next 2 lines.
If there might be "gaps" in the name, type, or time columns in the output as well, and in different combinations, it gets trickier.
You really need your program to have special knowledge about what values are valid in what columns. Eg, that 'Jessica' is not a valid type of exercise, and 'Jogging' is not a valid name.
Regarding formatting the output
Brian's answer is relevant.
It depends on the language you use. Most languages have a printf-equivalent. The formatting codes of printf allow you to pad with space, etc.
If you are using Perl (might be well-suited to this task), you can use formats