• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Neophyte Java question

EvilManagedCare

Senior member
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.
 
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.
 
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).
 
Back
Top