Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: aligning help

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default aligning help

    System.out.print("\nList 2 : ");
    for (i = 0 ; i<list.length ; i++){
        list2 [i] =(int)(99*Math.random()+1);
       System.out.printf(list2[i], " %2d");
    }


    i have this code... i want to align numbers

    it gives 10 random numbers,, i have 2 lists so for example..

    2 3 20 39 1 90 88 58 2 10
    12 23 8 12 54 2 9 1 58 2
    the out im coming up with my current code is that.... so i went for printf but no luck..

    the output must be
    2 3 20 39 1 90 88 58 2 10
    12 23 8 12 54 2 9 1 58 2

    perfect alignment help please...


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: aligning help

    Not very good with print f. Try removing that space in " %2d"
    and changing it to "%2d".

    You could also just have for each list:

    System.out.print("\nList 2 : ");
    for (i = 0 ; i<list.length ; i++){
    list2 [i] =(int)(99*Math.random()+1);
    System.out.print(list2[i] + " ");
    }

    That should work, but I'm not sure.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: aligning help

    it doesnt work because if a number with 1 digit comes up then the alignment will be all messed up... need help my instructor specifically told us to watch out for the alignments...

    i think its printf because i want to assign the integer with only 2 places so 1 digit numbers will be inputed like 02 or 01 but the zeros will not be outputted...

    and yeah the code you gave me that was my first try btw hehe

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: aligning help

    You're right. printf is the way to go. Read this API for the formatting codes:
    Formatter (Java Platform SE 6)

    db

Similar Threads

  1. Aligning Issues in the Output
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2010, 05:29 AM