Results 1 to 7 of 7
  1. #1
    rexson98 is offline Member
    Join Date
    Oct 2008
    Location
    Philippines
    Posts
    8
    Rep Power
    0

    Default aligning numbers,..

    pls help me in aligning numbers,.. this is my code,.. the numbers must be align,. thnx in advance guys!

    public class Table{
    public static void main(String args[]){

    try{

    for(int l=0;l<10;l++){
    System.out.print(" "+l );
    }
    System.out.println();
    for(int y=1;y>-10;y--){
    System.out.print(" "+y );
    }
    System.out.println();
    for(int m=2;m>-9;m--){
    System.out.print(" "+m);
    }
    System.out.println();
    for(int a=3;a>-8;a--){
    System.out.print(" "+a);
    }
    System.out.println();
    for(int r=4;r>-7;r--){
    System.out.print(" "+r);
    }
    System.out.println();
    for(int i=5;i>-6;i--){
    System.out.print(" "+i );
    }
    System.out.println();
    for(int x=6;x>-5;x--){
    System.out.print(" "+x );
    }
    System.out.println();
    for(int z=7;z>-4;z--){
    System.out.print(" "+z );
    }
    System.out.println();
    for(int q=8;q>-3;q--){
    System.out.print(" "+q );
    }
    System.out.println();
    for(int w=9;w>-2;w--){
    System.out.print(" "+w );
    }
    }catch(Exception e){}
    }
    }

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    What on Earth are you talking about? Aligned? Aligned to what? Where? Who? What output are you getting and what do you expect?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Are you talking about the println output? Do you want the output to be right adjusted with leading blanks?
    Decide on the longest number of digits(MaxDig) you are going to output.
    Convert the int to String.
    Pad the String on the left with blanks so that it has MaxDig total length.

  4. #4
    rexson98 is offline Member
    Join Date
    Oct 2008
    Location
    Philippines
    Posts
    8
    Rep Power
    0

    Default

    the output numbers must be aligned to the other one below like a multiplication table,.. but this is a subtraction table,.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You might want to read up on using System.out.printf(...) It will do what you want here.

  6. #6
    rexson98 is offline Member
    Join Date
    Oct 2008
    Location
    Philippines
    Posts
    8
    Rep Power
    0

    Default

    norm, can you show me the syntax to set maxdig? because i'm new to java

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Say you want to have 5 digit numbers and you also want a leading space:
    int MaxDig = 6; // set size of padded numbers to 6

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 04:03 PM
  2. problem aligning both image/text inside a button
    By rick_cols in forum SWT / JFace
    Replies: 0
    Last Post: 09-18-2008, 09:20 PM
  3. Having Trouble Aligning JLabels
    By Mark_Petrov in forum AWT / Swing
    Replies: 0
    Last Post: 01-20-2008, 06:22 PM
  4. Aligning Labels
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 07:26 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •