Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Table formatting???  RSS feed

 
Sarah Bushnell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help!!!

I'm new to Java, as of about 4 weeks ago. I've got all the basics OK, but we've been given a task to take a lot of information and put it into a non-graphical table. All of the tutorials I have found just talk about GUIs. Basically, they just want this information to look nice and arranged in rows and columns.

Any ideas, I'm stumped...

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Using JDK 1.5, System.out.printf() is your friend. This method will let you very simply print formatted lines of output; use tab characters ("\t") to separate fields.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also find it useful to pad your data with spaces instead of tabs. If some of your data is 3 characters long and some is 9 you may find tabs still jump around on the screen a bit. If you do much console or text file output you might like to make a couple little library routines with signatures like:

String left( String input, int width, char pad )
String right( String input, int width, char pad )

The names left & right are short for left justify and right justify. Specifying the pad character is optional but can be cool.

Printf also lets you specify widths. I keep forgetting about the new stuff.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!