?
Solved

right justify std output.

Posted on 2002-05-13
5
Medium Priority
?
1,643 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

650 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