0

I want to print status messages into Console.

   System.out.format("%-60s%10s\n", "Status 1", "[Success]");
            System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2 and some text", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Success]");
            System.out.format("%-60s%10s\n", "Status 2 and some very long text", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Success]");
            System.out.format("%-60s%10s\n", "Status 2 and some very very very long text", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Success]");
            System.out.format("%-60s%10s\n", "Status 2", "[Failed]");

But as you can see the second column messages are not formatted properly.

Status 1                                                     [Success]
Status 2                                                      [Failed]
Status 2                                                      [Failed]
Status 2 and some text                                        [Failed]
Status 2                                                      [Failed]
Status 2                                                     [Success]
Status 2 and some very long text                              [Failed]
Status 2                                                     [Success]
Status 2 and some very very very long text                    [Failed]
Status 2                                                     [Success]
Status 2                                                      [Failed]

I want to print the messages like this:

Status 1                                                      [Success]
Status 2                                                      [Failed]
Status 2                                                      [Failed]
Status 2 and some text                                        [Failed]
Status 2                                                      [Failed]
Status 2                                                      [Success]
Status 2 and some very long text                              [Failed]
Status 2                                                      [Success]
Status 2 and some very very very long text                    [Failed]
Status 2                                                      [Success]
Status 2                                                      [Failed]

How I can do this?

1
  • As a side note: it would be better to use %n instead of \n in the format string so that you get a newline sequence appropriate to the platform. Commented Dec 29, 2013 at 16:37

2 Answers 2

2

Why not add "-" to make the last field left-justified, as the first field? I suppose "%-60s%-10s\n" would work.

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

Comments

0

You Can try this code

  System.out.format("%-60s%.10s\n", "Status 1", "[Success]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2 and some text", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Success]");
    System.out.format("%-60s%.10s\n", "Status 2 and some very long text", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Success]");
    System.out.format("%-60s%.10s\n", "Status 2 and some very very very long text", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Success]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");

1 Comment

%.10s will let you print max 10 characters from passed String so maybe better idea would be just removing .10 and using simple %s. But in case OP will want to add some other informations after Success/Failed column it is better to make sure that this column has fixed size. In case we want to print string value justified left we should add - before column length.

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.