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.

Recommended Answers

All 4 Replies

I did use the "\t" (tabs).

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.