Solved

Alignment

Posted on 2007-03-29
14
310 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
 
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:mayankeagle
ID: 18826067
Why do that when you have API already existing to do that, dandeliondream :)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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:mayankeagle
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:mayankeagle
mayankeagle earned 100 total points
ID: 18831059
Yes you need Java 5
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18835139
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now