Solved

Alignment

Posted on 2007-03-29
14
315 Views
Last Modified: 2013-11-23
I'm using ms-dos to run my java program. I am working on listing a list of data as shown below:
ID      Name                  Age      Gender
---      ----------------------------------      -----      ------------
1001      John                  21      M
1023      Jass Lee   8  M

How do i align  Jass Lee's age and gender properly?
0
Comment
Question by:dandeliondream
  • 6
  • 5
  • 3
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 18816572
Try

      final String COLS = "%6s%16s%4s%7s\n";
      System.out.printf(COLS, "ID", "Name", "Age", "Gender");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18816576
System.out.printf(COLS, "1001", "Jass Lee", "21", "M");
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 18816738
can this work for java program? printf sounds like c programming to me
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 86

Expert Comment

by:CEHJ
ID: 18816772
Yes >= Java 1.5
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 18822420
is there other alternative?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 18822423
You'll have to applying padding to StringBuffer yourself
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18826067
Why do that when you have API already existing to do that, dandeliondream :)
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 18828503
can i use formatter class? i tried out bit it din work out.
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 18828510
This is what i am working on but i can't align the rows properly.

public void display(int tgt)
      {
            
            System.out.print(database[tgt].getID()+"   ");
            System.out.print(database[tgt].getName()+" ");
            System.out.print(database[tgt].getAge()+"  ");
            System.out.print(database[tgt].getGender()+"   ");
            System.out.print(database[tgt].getMobile()+" ");
            System.out.print(database[tgt].getCourseTitle()+" ");
            System.out.print(database[tgt].getFee()+"\n");
}
      
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18828811
>>can i use formatter class? i tried out bit it din work out.

That's no different to using System.printf
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18829113
Adding " " will not do it - you need tab spaces, or better use printf () as CEHJ suggested
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 18830815
I am using j2sdk1.4.2_13 to compile and run the java files. Is this the reason why i can't run System.printf?
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 18831059
Yes you need Java 5
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18835139
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 71
hibernate jars 4 45
Which non-HTML GUI front end to use with Java? 3 24
Running JavaFX on the Raspberry Pi 27 38
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

809 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