2

How do I format output by using printf.

suppose i want to print following in formatter.

testing testing testing testing
testingggg  testingggg  testingggg  testingggg

I am not asking about using "\t", I am asking on printf style formatting? If some one can give me suggestion and example. Or may be link with example. I can do it to fit my need.

2 Answers 2

10

I think you're looking for the Formatter class, which does printf-style formatting for Java.

For example, applied to your input:

    StringBuilder sb = new StringBuilder();
    Formatter formatter = new Formatter(sb, Locale.US);

    formatter.format("%1$-15s %2$-15s %3$-15s %4$-15s\n", "testing", "testing", "testing", "testing");
    formatter.format("%1$-15s %2$-15s %3$-15s %4$-15s", "testingggg", "testingggg", "testingggg", "testingggg");

    System.out.println(sb.toString());

Produces:

testing         testing         testing         testing        
testingggg      testingggg      testingggg      testingggg    
Sign up to request clarification or add additional context in comments.

Comments

0

You can use System.out.printf("%sggg" , t)

Where t = "testing" and ggg is just appended onto this string.

Hope this helps :D

1 Comment

no that was just example..i want to print them as if they are in table?

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.