adhg
adhg

Reputation: 10853

howto nicely format text on console when the output is longer than \t

How can I fix this formatting issue? I'm outputing text to my console and the \t is not aligned correctly (because the amount if too long). I can cheaply fix it with an 'if' statement that check the length but wonder if there's a better way to do this.

thanks

enter image description here

snap of code:

    //here comes the title/header (Dec-12.......Jan-13.......)

    for (int r=0; r<table.rowKeySet().size(); r++)
    {
        Map<Integer, SaleReportEntity> map = table.row(r);
        System.out.print(map.get(0).getSalePeriodStr()+"\t");

        for (int c=0; c<table.columnKeySet().size(); c++)
        {
            SaleReportEntity sre = map.get(c);
            System.out.print(sre.getTotalAmountStr()+"\t\t");
        }
        System.out.println(); //new line
    }

Upvotes: 0

Views: 502

Answers (1)

camickr
camickr

Reputation: 324098

I've never used it, but I believe the printf(...) method allows you to control spacing and formatting. Very simple example:

System.out.printf("%7s%7s%7s%7s", "a", "b", "c", "d");

Upvotes: 1

Related Questions