(4) A popular way of formatting output in Java is the print format
function, originated from the C and C++ languages, that takes the
following form:
The first parameter is a format string that specifies the format of the
output. Each format specifier starts with a percent sign (%) and
finishes with a letter that determines the type of output.
The remaining parameters, i.e., the arguments, are the values to be
printed out.
Unit 3 - Introduction to Java 84
System.out.printf(“String, [arguments]);
For example, when the following statement is executed
it produces the output:
The meanings of format specifier:
“10” – the minimum number of characters in the output
“2” – the number of digits after the decimal point
“f” – the output is in floating-point form
“6.02235” – the number to be printed
Unless specified otherwise, the number is right justified
Unit 3 - Introduction to Java 85
System.out.printf(“%10.2f”, 6.02235);
_ _ _ _ _ _ 6.02
Besides the floating point format, there are some common format
specifiers that are useful in Java programming
Unit 3 - Introduction to Java 86
Format specifiers can be combined with other characters in the
format string, making it quite handy to insert values into an output
string.
For example: The following snippet
yields the output:
Unit 3 - Introduction to Java 87
int x = 17, y = 42;
System.out.printf(“The product of %d and %d is %d”, x, y, x*y);
The product of 17 and 42 is 714
Example: Execute the following code
The output:
Unit 3 - Introduction to Java 88
double price = 19.5;
int quantity = 2;
String item = “Widgets”;
System.out.printf(“%10s sold:%4d at $%5.2f. Total = $%1.2f”,
item, quantity, price, quantity*price);
_ _ _ Widgets sold:_ _ _ 2 at $19.50. Total = $39.00
%10s %4d %5.2f
%1.2f
In addition, flags can be added to further control the appearance of
the formatted output.
Some common flags are tabulated below:
Unit 3 - Introduction to Java 89
For example, the following statements
yield the outputs
Yet
give
Unit 3 - Introduction to Java 90
System.out.printf(“%,1.2f”, 10000.0 / 3.0);
3,333.33
System.out.printf(“%, 1.2f”, 10000.0 / 3.0);
System.out.printf(“%,+1.2f”, 10000.0 / 3.0);
System.out.printf(“%,+1.2f”, -10000.0 / 3.0);
System.out.printf(“%,(1.2f”, -10000.0 / 3.0);
_ 3,333.33 +3,333.33
-3,333.33
(3,333.33)
(5) (Optional) Formatting output can be achieved using field widths.
There are three types of variables whose field widths can be adjusted.
(i) Formatting strings: A number after the string will create a left
justified partition.
For example, the line
gives the output below:
Unit 3 - Introduction to Java 91
print(“Strings, integer);
System.out.print(“Bob A”, 7);
Bob _ A _ _
Altogether seven spaces from the left
(ii) Formatting integers: a number after the integer will create a right
justified partition.
For example, the code below
generates the output
Think: What will be the output when the following line is run?
Unit 3 - Introduction to Java 92
System.out.print(123, 8);
_ _ _ _ _ 123
Altogether 8 spaces from the right
System.out.print(215, 6); System.out.print(56, 4);
(iii) Formatting decimals: the first number after the value will create a
right justified partition, and the second number is how many spaces
after the decimal to round to.
For example, the statement
prints out
The statement
on the other hand yields
Unit 3 - Introduction to Java 93
System.out.print(123.56, 8, 1);
_ _ _ 123.6
Altogether 8 partitions
1 decimal place
System.out.print(123.56, 9, 4);
_ 123.5600
Example: What will be the output when the following program is run?
Unit 3 - Introduction to Java 94
System.out.println(“12345678901234567890”);
System.out.print(“Hey”, 5);
System.out.print(“Whats Up”, 12);
System.out.println(“Doc”);
System.out.println(512, 5);
System.out.print(67.8, 6, 1);
System.out.println(765.987, 7, 2);
System.out.println(765.987, 8, 1);
System.out.println(765.987, 1, 3);
System.out.println(765.987, 10, 5);
Some reminders:
1. In formatting strings, integers or decimals, if the field width is too
small to fit in the assigned value, it will be ignored.
2. A string and a number cannot be formatted in the same print() or
println() statement.
Unit 3 - Introduction to Java 95
System.out.print(“Nice Day!”, 12);
System.out.print(“Nice Day!”, 5);
Nice _ Day! _ _ _
Nice _ Day!
System.out.println(“Hi there” + 67.89, 5, 1);
System.out.print(“Hi there”);
System.out.println(67.89, 5, 1);
Error!
Hi there 67.9
Note: The direct manipulation of field width requires a specific class
that is not included in the Java package, and is therefore not
applicable in our class.
Unit 3 - Introduction to Java 96
(h) Input
Unlike output which can be formatted rather straightforwardly,
reading input is quite tricky in Java.
In order to read console input, we need to construct a Scanner that is
attached to System.in:
Here in is the object that allows for console input. This object can
then be used in conjunction with various methods in Scanner class to
read input.
For example:
Unit 3 - Introduction to Java 97
Scanner in = new Scanner(System.in);
String name = in.nextLine();
The Scanner class is defined within the java.util package. Therefore, it
has to be imported at the beginning:
There are several common methods in Scanner class:
Unit 3 - Introduction to Java 98
import java.util.Scanner;
Method Function
nextLine() Read in the next line of input
next() Read in the next word of input
nextInt() Read in an integer
nextDouble() Read in a floating point value