Elad Benda
Elad Benda

Reputation: 36656

why is my string not in fixed size?

I try to print strings with padding (table like):

    StringBuilder sb = new StringBuilder();

    sb.append(String.format("%1$15s", "bulk name"));
    sb.append(String.format("  |  "));
    sb.append(String.format("%1$15s", "baseline"));
    sb.append(String.format("  |  "));
    sb.append(String.format("%1$15s", "current"));
    sb.append(String.format("  |  "));
    sb.append("\n");
    for (int i = 0; i < baseLine.tillOneSecondBulks.size(); i++) {

        sb.append(String.format("%1$15s", baseLine.tillOneSecondBulks.get(i).bulkName));
        sb.append(String.format("  |  "));
        sb.append(String.format("%1$15s", baseLine.tillOneSecondBulks.get(i)
                .count));
        sb.append(String.format("  |  "));
        sb.append(String.format("%1$15s", current.tillOneSecondBulks.get(i)
                .count));
        sb.append(String.format("  |  "));
        sb.append("\n");


        sb.append("\n");
    }
    sb.append("\n");
    sb.append("\n");

but it still comes not ordered:

      bulk name  |         baseline  |          current  |  
[0,0.2) seconds  |                0  |                0  |  

[0.2,0.4) seconds  |                0  |                0  |  

[0.4,0.6) seconds  |                0  |                0  |  

[0.6,0.8) seconds  |                0  |                1  |  

[0.8,1) seconds  |                1  |                0  |  

Upvotes: 0

Views: 162

Answers (1)

SMA
SMA

Reputation: 37023

Try increasing the length of header as well as each row for the first field. Reason if you see for e.g. [0.2,0.4) seconds is more than 15 characters long.

Try to increase it say to 20 characters and that would resolve the issue.

Upvotes: 1

Related Questions