[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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