• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1645
  • Last Modified:

right justify std output.

what is the simplest way to right justify the standard output in java ?
example :
on left hand side I want string variables from a String[].
on the right , its integers.(those I want right justified, so they appear under one column).

  • 3
1 Solution
Not really. You'd have to manually put \t (tabs) between your String and Int values so they appear to be in two cols. One \t is equivalent to not more than 8 spaces, so you have to loop over your string array to find out how many tabs you need to put in after each entry.
if your longest String entry is 12 chars and the current one is only two chars, you need to append two tabs (12 - 2 = 10, 10 \ 8 + 1 = 2).
Jim CakalicSenior Developer/ArchitectCommented:
If you have a background in C then you are likely to familiar with the printf-family of functions that provide formatted output capabilities including justification, blank-padding, and min/max field widths. The NumberFormat, DecimalFormat, and MessageFormat classes provide some, but not all, of the printf capabilities and tend to be difficult to work with. Some time ago, an implementation of printf capabilities was posted and described at the Java Developer Connection. You can find that article here:

Supposedly, these capabilities were to be added along with the new i/o facilities in JDK 1.4. If they were, I haven't been able to find them anywhere.

Best regards,
Jim Cakalic
Jim CakalicSenior Developer/ArchitectCommented:
Here's a quick example of PrintfFormat. Assume you had pairs of String and int values as command-line arguments. and you wanted to print them in columns with the Strings left-justified, blank-padded, and restricted to 15 spaces, and the ints right justified and left-padded to 10 spaces. Here is how you could do that:

public class PrintfTest {
    public static void main(String[] args) throws Exception {
        int i = 0;
        PrintfFormat stringFormat = new PrintfFormat("%-15.15s");
        PrintfFormat numberFormat = new PrintfFormat("%10i");
        while (i < args.length) {
            String label = args[i++];
            int n = Integer.parseInt(args[i++]);

Running the program like this:

> java PrintfTest label1 5 label2 13 label3 77834393 "long label --------" 0

would yield this output:

label1                  5
label2                 13
label3           77834393
long label ----         0

Notice that the last label is longer than 15 characters so it is truncated on the right to fit into the proscribed width.

Jim CakalicSenior Developer/ArchitectCommented:
Hmm. Well, columnized examples don't post very well in a proportional font. Forgot that :-(
angrishAuthor Commented:
Thanks Jim.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now