Neophyte Java question

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
I'm taking an introductory software engineering class that uses Java. I completed a homework assignment yesterday that involved using loops and printing results with printf.
We had to print out these columns, that was incredibly tedious, having to resort to printing a string of spaces to separate columns. I was thinking there had to be a better way. So, my question is, what are the ways to display tables of data in Java besides printing lines of text interspersed with lots of spaces? Is there a table object in the Java library that can be used? We haven't really touched on the visual programming aspect (for lack of a better term) yet, so I wasn't sure where to look.

Thanks for the help.
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
fmt = new Formatter();
fmt.format("%4d %4d %4d", i, i*i, i*i*i);
System.out.println(fmt);
This way it works just like C printf.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Cooler
fmt = new Formatter();
fmt.format("%4d %4d %4d", i, i*i, i*i*i);
System.out.println(fmt);
This way it works just like C printf.
Err, that's a little silly when printf is already implemented (System.out is a PrintStream). ;)

But yeah, the %4d is one option. The other is to insert '\t' characters (tabs) which will move the cursor over to the next multiple of 8 (although I suppose that can change, depending on your shell settings). Works as long as some items don't cross over the tab lines themselves (in which case you'd have to count tabs like you're currently counting spaces).