Hey all,

I have a problem with printing my output in a certain way from my Java program.

I actually need to print the output in a table-like structure.

For example:

=============================================================================
Header1    |    Header2   |    Header3    |   Header4     |     Header4 |
=============================================================================
Info here   |   Info here   |   Info here    |   Info here    |   Info here  |
Info here   |   Info here   |   Info here    |   Info here    |   Info here  |
Info here   |   Info here   |   Info here    |   Info here    |   Info here  |
=============================================================================

Now, I don't actually know how many rows the program will print (since it's a while loop) but I do know that there must be no more than 7 columns. There might be empty cells in a row (i.e: no info).

The problem I'm facing is the size of each "info" in a cell. I can't get the size of the cell to stay the same, it is dependent on the info being printed there.
So here's my problem:

=============================================================================
Header1 |       Header2   |    Header3  |    Header4     |     Header4 |
=============================================================================
Info here    |   Info here   |   Info here    |   Info here    |   Info here  |
Info here|   Info here  |  Info here  |   Info here    |   Info here  |
Info here |   Info here    |   Info here    |   Info here    |   Info here  |
=============================================================================

So briefly, I cannot keep the table being printing fixed. I'm using "\t" and I've tried white spaces (just " ").

Any suggestions would be greatly appreciated.

you could try have something like a MAXSIZE, or you could render the table after you have read all the info.

or use good old sprintf

new PrintfFormat("\'%8s\'").sprintf("text");

where 8 is desired string length
more here

This article has been dead for over six months. Start a new discussion instead.