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: System.out.print -align

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to align last digits

    Let me present my problem on this case:

    int number = 0;
    for(int i=1; i<11; i++){
    number = (int) (Math.random()* 100);
    System.out.println(" " + number);
    }
    Running this program in cmd, it simply prints out 10 numbers, each in new line. Their first digits are aligned.

    What am I trying to do is to align their last digits.

    I couldn't find any advanced parameters in "System.out.println" to set.
    Is there another "out-printing" method in java that I can use?

    (keep in mind, that I have to run this program in cmd)

    Any help would be very apriciated
    Last edited by juwan; November 13th, 2010 at 08:48 AM.


  2. #2
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Wink Re: System.out.print -align

    This is quite simple really, you just need to check to see the value of the number and insert chars or spaces before you print the number, ie, if you have the following numbers: 4 16 100, why not make then 004, 016 and 100? Here is a quick and dirty example of this in action with comments:
    // Okay, this is an example to line up numbers so that the last
    // or least significant number is aligned, let's see how we can
    // do this in Java, eh? First, we need to declare our class name
    // or something.
    public class Align
    //Right, let's get into the main routine
    {
        public static void main (String args [])
        {
            // Okay, let's set up some variables:
            int eye=0;
            int number=0;
            // Here is our loop:
            for (eye=1;eye<=10;eye=eye+1)
            {
                // Okay, we'll make the variable number a random
                // number less that 100? Or will it be less than
                // 101? I can't remember, but in some languages,
                // if you want an integer number to 100 then you
                // have to specify 101, so to be on the safe
                // side, and assuming that we want a number up to
                // 100, we'll ask for a number up to 101:
                number=(int)(Math.random()*101);
                // Right, let's start off with a space using print,
                // print will not put a line feed after it, which
                // we'll do when we've actually printed the number
                // out with 'println':
                System.out.print(" ");
                // Here are our conditions so...
                if (number<10)
                {
                    // we'll put two digits in front of the number
                    System.out.print("00");
                    // or...
                }
                else if (number>=10 && number<100)
                {
                    // we'll put one digit in front of the number
                    System.out.print("0");
                }
                // now, we'll print the number with a line feed after it
                System.out.println(number);
                // Just a note: I'm going to assume that you may replace
                // the nuaghts with spaces to align the numbers to the
                // right :-) Enjoy! Shaun Bebbington MMX
            }
        }
    }
    Simple, eh?

    Regards,

    Shaun.

  3. #3
    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: System.out.print -align

    Read the API for String#format(...) and PrintStream#printf(...). And you'll want to to get the details of formats from Formatter.

    db

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: System.out.print -align

    Alternatively, you can use printf, which is used in C.

    When using printf, you use placeholders which you can format. The placeholder for int is %d. If you want to indicate that all numbers should have 2 spaces, you use the placeholder %2d. If you want all numbers to hold 2 spaces and have a leading zero if the space(s) in front of the number would otherwise be just an empty space, you use the placeholder %02d.

    So, if you want all of your numbers to be aligned without leading zeros, your code would look like this:

    int number = 0;
    for(int i=1; i<11; i++){
    number = (int) (Math.random()* 100);
    System.out.printf("%2d", number);
    }

    If you want all of your numbers to be aligned with leading zeros, your code would look like this:
    int number = 0;
    for(int i=1; i<11; i++){
    number = (int) (Math.random()* 100);
    System.out.printf("%02d", number);
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. How can i print rows 1 by 1 using System.out.println?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 26th, 2010, 07:35 AM
  2. Screwed up system variables
    By paisa90 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 19th, 2010, 11:56 PM
  3. remote system shutdown
    By prince joseph in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 28th, 2010, 02:35 AM
  4. System.CurrentTimeMilliseconds
    By Dave in forum Java SE APIs
    Replies: 1
    Last Post: August 26th, 2009, 11:02 AM
  5. [SOLVED] Mobile operating system using Java technology
    By blackJava in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 16th, 2009, 02:44 PM