0

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?

2
  • 1
    Did you tryed to format your string with \t? its "tab" .. shoud get you the expected output. Commented Jan 21, 2014 at 19:43
  • @TomasBisciak: I tried tab,but that is giving separation for the headers but not for the rows below it Commented Jan 21, 2014 at 19:51

2 Answers 2

1

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.

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

3 Comments

when you say String.format("%-40s %4d%n", "Total Billable:", 40);, you specify that Total Billable must be extreme left end followed by padding with spaces in total for 40. is my understanding correct?
Yes, that's correct. It left justifies the input and pads it to the total specified. Note, it left justifies based on the previous entry, so you could also add '-' flag to the second parameter and it will left justify based on the first parameter and pad accordingly.
I changed %-40 to %-42 so that I could center 40 with Hours header. and it looks much better..
1
        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.

2 Comments

this looks much better. one question though is the numbers are not centered (they are left-aligned with headers)..can you see that?
There might be limitation on what you can do with that string in console.If you want to you can do it by algorithm and play with spaces ,string formating.Bud that woud cost you more processing power.At least thats what i woud do if i really woud need to do that the way you want to.Not sure if there is any easy way to align them to right without any string manipulation

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.