Problem solving and Projectile Motion, ch.7 Flashcards
%c
Data Type:
char
Prints a single Unicode character
%d
int, long, short
Prints a decimal integer value.
%o
int, long, short
Prints an octal integer value.
%h
int, char, long, short
Prints a hexadecimal integer value.
%f
float, double
Prints a floating-point value.
%e
float, double
Prints a floating-point value in scientific notation
%s
String
Prints the characters in a String variable or literal
%%
Prints the “%” character.
%n
Prints the platform-specific new-line character.
Floating-point formatting.
Method calls to printf() apply to PrintStream objects like System.out.
Sub-specifier: width
printf(“Value: %7.2f”, myFloat);
Value: 12.34
Specifies the minimum number of characters to print. If the formatted value has more characters than the width, the value will not be truncated. If the formatted value has fewer characters than the width, the output will be padded with spaces (or 0’s if the ‘0’ flag is specified).
Floating-point formatting.
Method calls to printf() apply to PrintStream objects like System.out.
Sub-specifier: .precision
printf(“%.4f”, myFloat);
12.3400
printf(“%3.4e”, myFloat);
1.2340e+01
Specifies the number of digits to print following the decimal point. If the precision is not specified, a default precision of 6 is used.
Floating-point formatting.
Method calls to printf() apply to PrintStream objects like System.out.
Sub-specifier: flags
printf(“%+f”, myFloat);
+12.340000
printf(“%08.2f”, myFloat);
00012.34
-: Left aligns the output given the specified width, padding the output with spaces.
+: Prints a preceding + sign for positive values. Negative numbers are always printed with the - sign.
0: Pads the output with 0’s when the formatted value has fewer characters than the width.
space: Prints a preceding space for positive value.
float myFloat = 45.1342f;
System.out.printf(“%.3e”, myFloat);
4.513e+01
float myFloat = 45.1342f;
System.out.printf(“%+.5f”, myFloat);
+45.13420
float myFloat = 45.1342f;
System.out.printf(“%09.2f”, myFloat);
000045.13
int myInt = -713;
printf(“%+04d”, myInt);
-713
myInt is a negative value, so the + sign is not printed. A - sign is printed for all negative values.
int myInt = -713;
printf(“%05d”, myInt);
-0713
The 5 indicates 5 characters printed: one character for the - sign, one character for the 0 used to pad the printed value, and 3 characters for the value 713.
int myInt = -713;
printf(“%+02d”, myInt);
-713
myInt is a negative value, so a - sign is printed for all negative values. The value -713 is 4 characters, more characters than the specified width of 2, so the value is not truncated.
String myString = “Testing”;
printf(“%4s”, myString);
“Testing”
Strings longer than the specified width are printed without truncation.
String myString = “Testing”;
printf(“%8s”, myString);
” Testing”
Strings shorter than the specified width are printed with spaces. “Testing” requires 7 characters, so 1 space is added as padding.
String myString = “Testing”;
printf(“%.4s”, myString);
“Test”
“Testing” is longer than the specified precision and is truncated to 4 characters.
String myString = “Testing”;
printf(“%.10s”, myString);
“Testing”
A maximum of 10 characters can be printed, but “Testing” has only 7 characters.
String myString = “Testing”;
printf(“%-8s123”, myString);
“Testing 123”
“Testing” has 7 characters, so the 7 characters of “Testing” are output followed by 1 space as padding. The - flag outputs the padding after the string.
How to write for an int
System.out.printf(“%1d\n”, myInt);
How to write for a double
System.out.printf(“%.2f\n”, myDouble1);
How to write for a string
System.out.printf(“%2s\n”, myString);
System.out.printf(“%-7s|\n”, name1);