• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

Alignment

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
dandeliondream
Asked:
dandeliondream
  • 6
  • 5
  • 3
2 Solutions
 
CEHJCommented:
Try

      final String COLS = "%6s%16s%4s%7s\n";
      System.out.printf(COLS, "ID", "Name", "Age", "Gender");
0
 
CEHJCommented:
System.out.printf(COLS, "1001", "Jass Lee", "21", "M");
0
 
dandeliondreamAuthor Commented:
can this work for java program? printf sounds like c programming to me
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
CEHJCommented:
Yes >= Java 1.5
0
 
dandeliondreamAuthor Commented:
is there other alternative?
0
 
CEHJCommented:
You'll have to applying padding to StringBuffer yourself
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Why do that when you have API already existing to do that, dandeliondream :)
0
 
dandeliondreamAuthor Commented:
can i use formatter class? i tried out bit it din work out.
0
 
dandeliondreamAuthor Commented:
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
 
CEHJCommented:
>>can i use formatter class? i tried out bit it din work out.

That's no different to using System.printf
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Adding " " will not do it - you need tab spaces, or better use printf () as CEHJ suggested
0
 
dandeliondreamAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
Yes you need Java 5
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now