Solved

right justify std output.

Posted on 2002-05-13
5
1,634 Views
Last Modified: 2013-12-14
Hi,
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).

0
Comment
Question by:angrish
  • 3
5 Comments
 
LVL 4

Expert Comment

by:pellep
ID: 7006723
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.
ie
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).
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7006762
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:
    http://developer.java.sun.com/developer/technicalArticles/Programming/sprintf/

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
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 100 total points
ID: 7006783
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++]);
            System.out.print(stringFormat.sprintf(label));
            System.out.print(numberFormat.sprintf(n));
            System.out.println();
        }
    }
}

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
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7006785
Hmm. Well, columnized examples don't post very well in a proportional font. Forgot that :-(
0
 

Author Comment

by:angrish
ID: 7026654
Thanks Jim.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 68
custom annotations 9 32
servlet  URL Rewriting 1 27
eclipse apache tomcat admin console 52 66
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now