Reputation: 267
I have a question on how to use printf for multiple formats. I want something like this, where everything inside printf is spaced out evenly:
i i i i i
and this is how I used printf:
// display headers
System.out.printf("", "%15s", "%15s", "%15s", "%15s", "Car Type",
"Starting Miles", "Ending Miles", "Distance/Gallon",
"Gallons/Distance", "Miles/Gallon");
when I executed my program, the above wouldn't show up at all, I just go this:
run:
Gas & Mileage Calculations
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BUILD SUCCESSFUL (total time: 0 seconds)
Upvotes: 1
Views: 3391
Reputation: 1911
The way you use printf()
is wrong.
It's first parameter is String format
, which will be the formatting string, which implements all further arguments.
Example:
String name = "Chris";
int age = 25;
System.out.printf("%s = %d", name, age);
This will print out "Chris = 25", because %s
is converted into the variable name
and %d
into variable age
. Check the API docs for more information.
Upvotes: 0
Reputation: 500197
Just concatenate the formats:
System.out.printf("%15s%15s%15s%15s", "header 1", "header 2", ...);
Upvotes: 1