Solved

Alignment

Posted on 2007-03-29
14
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
hibernate example for saving data 19 77
import as existing maven project 3 40
Html split(text) 2 31
Java pass by reference 3 21
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

733 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