aligning numbers,..

• 10-08-2008, 04:09 PM
rexson98
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){}
}
}
• 10-08-2008, 05:29 PM
ojn
What on Earth are you talking about? Aligned? Aligned to what? Where? Who? What output are you getting and what do you expect?
• 10-08-2008, 06:00 PM
Norm
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.
• 10-09-2008, 05:42 AM
rexson98
the output numbers must be aligned to the other one below like a multiplication table,.. but this is a subtraction table,.
• 10-09-2008, 05:43 AM
Fubarable
You might want to read up on using System.out.printf(...) It will do what you want here.
• 10-09-2008, 05:53 AM
rexson98
norm, can you show me the syntax to set maxdig? because i'm new to java
• 10-09-2008, 01:59 PM
Norm
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