Solved

Alignment

Posted on 2007-03-29
14
311 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:Mayank S
ID: 18826067
Why do that when you have API already existing to do that, dandeliondream :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
@SBGen Method 3 38
eclipse compiler vs Installed JREs option 3 77
servlet  URL Rewriting 1 28
difference of if loops 23 43
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

863 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

22 Experts available now in Live!

Get 1:1 Help Now