[CSC 204] Formatting Integer Output
Andrew J. Pounds
pounds_aj at mercer.edu
Wed Mar 18 13:24:10 EDT 2009
Here is some code demonstrates how to use the printf command to format
integers -- feel free to use this in your code if you have not already
done something else...
public class FormatOutput{
public static void main(String args[]){
// Examples of formatting integers using printf
int a = 2;
int b = 10;
int c = 120;
// Use 3 spaces for the number
System.out.printf("The number is %3d\n", a);
System.out.printf("The number is %3d\n", b);
System.out.printf("The number is %3d\n", c);
// Use 4 spaces for the number
System.out.printf("The number is %4d\n", a);
System.out.printf("The number is %4d\n", b);
System.out.printf("The number is %4d\n", c);
// Use 8 spaces for the number
System.out.printf("The number is %8d\n", a);
System.out.printf("The number is %8d\n", b);
System.out.printf("The number is %8d\n", c);
// The \n above does the same thing as System.out.println()
}
}
--
Andrew J. Pounds, Ph.D. (pounds at theochem.mercer.edu)
Associate Professor of Chemistry and Computer Science
Mercer University, Macon, GA 31207 (478) 301-5627
More information about the csc204
mailing list