# printf

• 11-12-2010, 01:17 PM
razzle
printf
Heya, I'm doing an assignment and need to print an array in:

System.out.println(numbers[0] + " " + numbers[1] + " " + numbers[2] + " " + numbers[3] + " " + numbers[4]);
System.out.println(numbers[5] + " " + numbers[6] + " " + numbers[7] + " " + numbers[8] + " " + numbers[9]);
System.out.println(numbers[10] + " " + numbers[11] + " " + numbers[12] + " " + numbers[13] + " " + numbers[14]);
System.out.println(numbers[15] + " " + numbers[16] + " " + numbers[17] + " " + numbers[18] + " " + numbers[19]);
System.out.println(numbers[20] + " " + numbers[21] + " " + numbers[22] + " " + numbers[23] + " " + numbers[24]);
System.out.println(numbers[25] + " " + numbers[26] + " " + numbers[27] + " " + numbers[28] + " " + numbers[29]);
System.out.println(numbers[30] + " " + numbers[31] + " " + numbers[32] + " " + numbers[33] + " " + numbers[34]);
System.out.println(numbers[35] + " " + numbers[36] + " " + numbers[37] + " " + numbers[38] + " " + numbers[39]);
System.out.println(numbers[40] + " " + numbers[41] + " " + numbers[42] + " " + numbers[43] + " " + numbers[44]);
System.out.println(numbers[45] + " " + numbers[46] + " " + numbers[47] + " " + numbers[48] + " " + numbers[49]);

this sort of format, but each value needs to be three digits long with leading zeros, I've found out that by using the formula (i.e. 3 goes to 003, or 10 to 010)
System.out.printf("%03d", numbers[0]);
it will do that, but i cant seem to get more than one on each line, is this possible?

Note: numbers is the name of the array.

• 11-12-2010, 02:03 PM
funnyboy
You just loop through it:

int count= 0;
for(count==0; count<=numbers.lenght; count++){
System.out.printf("%d, ", numbers[count]);
}
• 11-12-2010, 02:08 PM
razzle
I would do that but its got to be in a 5*10 grid. can you do that in a for loop?
• 11-12-2010, 02:34 PM
Fubarable
Quote:

Originally Posted by razzle
I would do that but its got to be in a 5*10 grid. can you do that in a for loop?

.........Yes
• 11-12-2010, 03:13 PM
funnyboy
Just initialze a second count variable and everytime it is 5, move to the next line!