Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.
Getting Ready: Before going any further, you should:
format() method in the
String
class can be used to create
formatted String objects. It must be passed a format
string and 0 or more values/objects to be formatted. In its
simplest form, the format string contains a single format specifier
with the following syntax:
%conversion
where the conversion can be, among other things: b for a boolean,
s for a String,
d for an integer (base 10), and f for a floating point number.
FormattingOutputDriver.
d to f in
the second call to format().
FormattingOutputDriver.
f back to d in
the second call to format().
%[width][.precision]conversion
where the width indicates the minimal field width and the
precision indicates the number of digits after the decimal
place for floating point numbers and the maximum number of characters
for String objects.
FormattingOutputDriver
so that all of the values are printed in a field of width 10.
FormattingOutputDriver.
format()
so that the resulting String has two digits to the right
of the decimal point.
FormattingOutputDriver.
format()
so that the resulting String has at most two characters.
FormattingOutputDriver.
%[flag]...[width][.precision]conversion
For example, a - causes
the resulting String to be left-justified, a + causes
the resulting String to include a sign (for numerical
values), and a , will include grouping separators (for numeric
values).
String would be returned by
String.format("%-10.2s", "CS239")? (Hint: Add the
approrpiate code to FormattingOutputDriver and see.)
String would be returned by
String.format("%,5.2f", 12345.67)?
String would be returned by
String.format("%,20.2f", 12345.67)?
String would be returned by
String.format("%-,20.2f", 12345.67)?
format() method can be passed multiple values/objects
after the format string. To accomodate this, the format string can contain
multiple format specifiers. It can also contain character literals.
String would be returned by
String.format("Sections: %d and %d", 1, 2)?
String would be returned by
String.format("CS%3.3s", 239)?
String would be returned by
String.format("2008: $%,10.2f\n2009: $%,10.2f", 92315.50, 105000.10);?
String
before "printing" it. In such cases, it is often convenient to use
the printf() method in the
PrintStream
class. Like the format()
method, the printf() method is passed a format string
and 0 or more values/objects to print.
System.out.printf("Sections: %d and %d", 1, 2);
System.out.printf("%,20.2f", 12345.67);
System.out.printf("Sections: %d and %d\n", 1, 2);
System.out.printf("%,20.2f", 12345.67);
format() method in the String
class and the printf() method int the PrintStream
class actually use a Formatter
object to do the work. Hence, the documentation for this class
contains all of the details of format strings and specifiers.
String/output to include
a percent sign?
Copyright 2011