Solved

Alignment

Posted on 2007-03-29
14
313 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 67
session migration servlets 2 39
oracle 11g 23 85
Notify sent to other threads in Java 9 16
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

831 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