Hi,
Any way i can format this output into columns?
for(int i = 0; i < 5; i++) { System.out.println(t[i]+n[i]+s[i]); }
Best Regards
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
Any way i can format this output into columns?
for(int i = 0; i < 5; i++) { System.out.println(t[i]+n[i]+s[i]); }
Best Regards
Look into System.out.prinf().
GregBrannon (March 1st, 2014)
Just outputs all messed up o.O
Did you happen to look at any documenation before trying to use printf? It isn't used exactly like the other print commands so System.out.printf(t[i]+n[i]+s[i]) would definitely not give you what you are looking for. For that matter, not posting what you used in your code won't help to get an answer to the issue. However, if you look for some info on printf I'm sure you can work it out.
NOTE: System.out.format() can do the same thing.
Last edited by KucerakJM; March 1st, 2014 at 02:06 PM.
GregBrannon (March 1st, 2014)
I did, but when I use them, all i get is the t[i] written in a line :/
for(int i = 0; i < 5; i++) { System.out.printf(title[i],"%20s", name[i], "%20s",subAmounts[i]); }
Alright, that clears some things up. That isn't quite the correct way to use printf()'s formatting capabilities. The basic syntax would be:
System.out.printf("format string", data1, data2, data3.....);
And this is what you are trying
System.out.printf(data1, "format string", data2, "format string", data3);
It should be noted that all formating information will be contained in the first argument for printf.
GregBrannon (March 1st, 2014)
and would this work, if t[i] are Strings of different lengths? What is happening, is that my output is shifting here and there.
Capture.PNG
This is the Format string i used: String formatString = " %-15s %20s %20.1f %n";
Your format string, as it stands, creates a 15 character left justified column(type string); a 20 character right justified column (type string); then another 20 character right justified column (type float); last a new line (basically). If there are different lengths then you want to make the column big enough to accomodate the largest length, plus some for a bit of padding if the next column is also left justified which is probably what you want.
GregBrannon (March 1st, 2014), melki0795 (March 1st, 2014)
thanks man, I got it, I always used to find formatting a bitch heh.
Thanks