?
Solved

right justify std output.

Posted on 2002-05-13
5
Medium Priority
?
1,640 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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:
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 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