I’m writing a file translator for my company that grabs data from a source file and writes a bunch of delimited records to a target file. The records have the form:
HEADER*REC 1*REC 2*REC 3*REC 4
If a record is empty, and there is another record that can come after it, then the value is not printed, but the delimiter is included, e.g.:
HEADER*REC 1**REC 3*REC 4
If a record is empty, and it is the last record in the series, then the value and the delimiter are omitted, e.g.:
HEADER*REC 1*REC 2*REC 3
I was trying to think of a nice way to describe this in code, other than (pseudocode):
if last record is empty
print this
otherwise
print this other thing
I guess the code isn’t too ugly, but I’d like a nicer solution. I’m using a StringBuilder to write the data for each transaction (each set of records corresponds to a transaction, so I can iterate through a TransactionSet Object.), and if I can, I try to avoid copious switch/if statements. If anyone knows of a more nicer, or elegant way to do this I would love to hear it.
EDIT: Clarified block of pseudocode
You can do it like this
This way it will only print a “*” if you have a heading to come after it.