printf

advertisement
The Print Formatting Statement
… named printf
Introduction to printf statements
print and println statements don’t allow us to easily format output
to the screen or a file. However, a printf statement does.
Just like the ln in println is an abbreviation for line. The f in
printf is an abbreviation for format.
printf statements allow us to left justify output or right justify
output, separately or we can use both in the same statement
to do things like align output in columns. This makes the
output more readable to the user of a program.
There are a number of things to understand before we start
using them, so let’s take our time and understand all the
different parts of a printf statement so we will know how to
write them correctly.
2
The Form of a printf Statement
A printf statement always has the following form:
System.out.printf ("format string", dataValue1, dataValue2, ....);
Notice the format string and all data values are separated by commas
NOT + signs.
The format string is inside double quotes and there must be a format
specifier in it for each data value listed in the remainder of the
statement. You can have any number of data values, but you must
have one format specifier for every data value. The data values are
NOT within double quotes.
Format specifiers always begin with the % symbol.
3
The printf Format Specifiers
There are three format specifiers that we will use:
the letter d for int values as in %5d
the letter f for floating point values as in %8.2f
the letter s for string values as in %10s
Also %n tells Java to display a new-line character. You can
also use the escape sequence \n.
If a dash - follows the % symbol of a format specifier, then the
data value will be left justified. If there is no dash then the
data value is right justified. The - is called a format flag.
4
The Format Specifier for int
printf statements can right justify integer output. Consider this code:
int fahrenheit = 212;
System.out.printf("%10d”, fahrenheit);
The printf statement right
justifies the value stored in the
variable fahrenheit in a field
width of 10 spaces.
Once java sees the format specifier %10d then it retrieves the int data value
stored in the variable fahrenheit (212). Java figures out that it needs 3
spaces to print 212 and then skips 7 spaces from the left margin and then
begins printing 212. It prints 212 in the 8th, 9th, and 10th spaces of the
field of 10 spaces. If we use a dash character ‘-’ to represent a blank
space, then the output would look like the following:
-------212
5
Right Justified Output for int
Here are random integers between 1 and 2000 that are right justified using a
prinf statement with a field width of 6. Notice the ones digits,tens digits,
hundreds digits, and thousands digits are lined up.
1102
295
1493
536
3
424
47
1983
1995
740
6
Left Justification for int
You can use a printf statement to left justify output also. Consider the code:
int fahrenheit = 212;
System.out.printf("%-10d”, fahrenheit);
The printf statement uses the
- format flag to left justify the
value stored in the variable
fahrenheit in a field width of
10 spaces.
Once java sees the format specifier %-10d then it retrieves the data value
stored in the variable fahrenheit (212). Java figures out that it needs 3
spaces to print 212 and then begins printing 212 at the left margin. It then
skips 7 spaces. It prints 212 in the 1st, 2nd, and 3rd spaces of the field of
10 spaces. The output would look like the following:
212-------
7
The Format Specifier for double
printf statements can be set to round what is printed out. They don’t actually
round what is in the variable, but they round what is seen on the screen.
In this example, we will print right justified a floating point value in a field
width of 8 with a precision of 4.
double percentage = 99.3456789;
System.out.printf("%10.4f”, percentage );
Once java sees the format specifier %10.4f then it retrieves the data value
stored in the variable percentage (99.3456789). Java figures out that it
needs 4 spaces to print the part of the number to the right of the decimal
point. It uses the remaining spaces: 10 - 4 = 6 to print the decimal point
and the part of the number to the left of the decimal. Note how the number
is rounded when it is printed.
The output would look like the following:
---99.3457
8
Left Justification for double
We can use a printf statement to round but use left justification instead. In this
example, we will print left justified a floating point value in a field width of 8
with a precision of 4.
double percentage = 99.3456789;
System.out.printf("%-10.4f”, percentage );
Note how the number is rounded and left justified when it is printed. Any
other output will start after the 3 spaces.
The output would look like the following:
99.3457---
9
The Format Specifier for string
printf statements can be used to display string information in quite a few
number of ways. In this example, we will print right justified a string value
in a field width of 20.
String phrase = “Java is Cool!”;
System.out.printf("%20s”, phrase);
Once java sees the format specifier %20s then it retrieves the data value
stored in the variable phrase(“Java is Cool!”). Java figures out that it
needs 13 spaces to print the phrase. It uses the remaining spaces: 20 13 = 7 to print 7 spaces before the phrase.
The output would look like the following:
------- Java is Cool!
10
Left Justification for string
In this example, we will print left justified a string value in a field width of 20.
String phrase = “Java is Cool!”;
System.out.printf("%-20s”, phrase);
Once java sees the format specifier %-20s then it retrieves the data value
stored in the variable phrase(“Java is Cool!”). Java figures out that it
needs 13 spaces to print the phrase. It uses the remaining spaces: 20 13 = 7 to print 7 spaces after the phrase. Any other output has to begin
after those 7 spaces.
The output would look like the following:
Java is Cool!-------
11
The New Line Format Specifier
None of the previous examples start a new line after printing the data
values. To start a new line after printing the data value, the new
line format specifier %n must be included in the printf statement.
All of the lines below will print the data values and start a new line.
System.out.printf("%10d%n”, fahrenheit);
System.out.printf("%-10d%n”, fahrenheit);
System.out.printf("%10.4f%n”, percentage );
System.out.printf("%-10.4f%n”, percentage );
System.out.printf("%20s%n”, phrase);
System.out.printf("%-20s%n”, phrase);
Note: when Java sees the %n format specifier, it doesn’t look for a
data value like it does with other format specifiers.
12
Multiple Format Specifiers in One Line
All of the previous printf statements have only one data value that is
formatted whether a new line is created or not.
Numerous format specifiers and data values can be included in one
printf statement. All of the format specifiers must appear in the
format string.
System.out.printf("%10d%10.4f%20s%n”, fahrenheit, percentage, phrase);
The output all on one line would look like this:
-------212---99.3457------- Java is Cool!
13
Multiple Format Specifiers in One Line
Sometimes we want to have numerous format specifiers and data values in one printf
statement, but we want to also print out additional string information. Here is an
example of how you might do that:
for (???”)
{
double square = ???;
double cube = ???;
System.out.printf("The square of %5.1f is %6.1f and the cube is %7.1f%n", i, square, cube);
}
The output all on one line would look something like this:
The square of
The square of
The square of
The square of
The square of
14
1.0
2.0
3.0
4.0
5.0
is 1.0 and the cube is 1.0
is 4.0 and the cube is 8.0
is 9.0 and the cube is 27.0
is 16.0 and the cube is 64.0
is 25.0 and the cube is 125.0
Ch 8 Sect 4 The String.format Statement
•
•
•
Sometimes we may want to format text but not print it out.
We can do this by using String.format and storing the
formatted string in a string variable.
Look at how we substitute String.format for System.out.prinf
and store the formatted string in a String variable.
double dollars = 25;
double tax = dollars * 0.125;
String s1 = String.format(“Income: $%.2f%n”, dollars);
String s2 = String.format(“Tax owed: $%.2f%n”, tax);
•
Why would we do this? For example, we might want to
format columns in a JTextArea in a GUI program. When we
send the string to the JTextArea, we want it to be nicely
formatted. We would only use System.out.printf if we were
going to display things in the console window.
Download