I am trying to print multiple arrays on one .txt file having one array print then have another column crated and have another array print how do i format this to work??
I cant remember the formatting commands to do this I need all the columns to align right now i have this
private static void makeFile(String[] name, String[] nickname, String[] capital,
String[] flowers, String[] population) throws FileNotFoundException
{
PrintWriter out = new PrintWriter ("out.txt");
for (int i = 0; i< 50 ; i++)
out.println( name[i] +" \t "+ nickname[i] );
out.close();
}
This is what prints
How do i fix it so they are all aligned and there are 3 more columns to add to this how do i get them to align as well ??
You should use String.format
like
String.format("%-30s %s", name[i], nickname[i])
where 30 is maximum length of name.
You can use printf
instead of println
, with C-style format strings.
out.printf("%-16s%-24s\n", name[i], nickname[i]);
This will print the name[i]
left-aligned in a 16-character placeholder, then print nickname[i]
in a 24-character wide column. When adding more columns, you can specify the maximum required number of characters in the format string. The -
sign is added for aligning the strings to left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With