1

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)
1
  • Check my answer to see, why your try prints out nothing. Commented Nov 28, 2013 at 10:20

2 Answers 2

1

Just concatenate the formats:

System.out.printf("%15s%15s%15s%15s", "header 1", "header 2", ...);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.