0

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
    }
adhg
  • 8,947
  • 8
  • 51
  • 88
  • http://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java – guido May 24 '13 at 15:17
  • 1
    You *may* want to look at `DecimalFormat` class: http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html – PM 77-1 May 24 '13 at 15:18

1 Answers1

1

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");
camickr
  • 308,339
  • 18
  • 152
  • 272