Question

I have this program to print the data in desired format. but I am having difficulty to get it working.

here is my code and output:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TestingStuff {

    public static void main(String[] args){
        Calendar calendar = Calendar.getInstance();
        calendar = new GregorianCalendar(2014, 07, 18);
        Date startDate = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
        String formatted = simpleDateFormat.format(startDate);
        String result="";
        String header2=String.format("%s %20s %20s %20s%n", "Account","Date","Hours","Skill");
        result+=header2;
        result+="-----------------------------------------------------------------------\n";
        result+=String.format("summary %20s%n",formatted);
        result+=String.format("Totabl Billable: %25s%n", 40);
        result+=String.format("Total Non-Billable: %25s%n", 0);
        result+=String.format("Total Hours %25s%n", 40);
        System.out.println(result);

    }

}

Output:

Account                 Date                Hours                Skill
-----------------------------------------------------------------------
summary             08/18/14
Totabl Billable:                        40
Total Non-Billable:                         0
Total Hours                        40

Expected output:

    Account                 Date                Hours                Skill
    -----------------------------------------------------------------------
    summary                08/18/14
    Totabl Billable:                              40
    Total Non-Billable:                            0
    Total Hours                                   40

As you can see the numbers are not centered with respect to tab headers. I am also hard coding the number of spaces such as (%20s) which I guess is not the right way. Is there a way to tell to format automatically with centering?

Was it helpful?

Solution

I would recommend using the java.util.Formatter '-' flag for Left Justify. From the Javadoc:

'-' '\u002d' Left justifies the output. Spaces ('\u0020') will be added at the end of the converted value as required to fill the minimum width of the field.

So, if we apply this to your example:

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    calendar = new GregorianCalendar(2014, 07, 18);
    Date startDate = calendar.getTime();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
    String formatted = simpleDateFormat.format(startDate);
    String result = "";
    String header2 = String.format("%s %20s %20s %20s%n", "Account", "Date", "Hours", "Skill");
    result += header2;
    result += "-----------------------------------------------------------------------\n";
    result += String.format("summary %20s%n", formatted);
    result += String.format("%-40s %4d%n", "Totabl Billable:", 40);
    result += String.format("%-40s %4d%n", "Total Non-Billable:", 0);
    result += String.format("%-40s %4d%n", "Total Hours:", 40);
    System.out.println(result);
}

Will pad the first input (the row label) with spaces to match a fixed width (40 in this case). This will produce:

Account                 Date                Hours                Skill
-----------------------------------------------------------------------
summary             08/18/14
Totabl Billable:                           40
Total Non-Billable:                         0
Total Hours                                40

This gives the added benefit of working for dynamic row labels since it automatically pads to the specified width regardless of the length of the label. It will be more flexible then trying to calculate the space for every possible label.

OTHER TIPS

        String header2=String.format("%s\t\t\t%s\t\t\t%s\t\t\t%s\n", "Account","Date","Hours","Skill");
        result+=header2;
        result+="-------------------------------------------------------------------------------\n";
        result+=String.format("summary \t\t%s\n",formatted);
        result+=String.format("Totabl Billable: \t\t\t\t%s\n", 40);
        result+=String.format("Total Non-Billable: \t\t\t\t%s\n", 0);
        result+=String.format("Total Hours \t\t\t\t\t%s\n", 40);
        System.out.println(result);

this shoud work.Worked for me even when i put different numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top