Solved

right justify std output.

Posted on 2002-05-13
5
1,633 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 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

15 Experts available now in Live!

Get 1:1 Help Now