Console Output
Download
Report
Transcript Console Output
Console Output in Java
Notes Supplement
CS 1054 Introduction to Programming in Java
Spring 2008
print() and println()
So far, we’ve used System.out.print and
System.out.println for output
With a single argument
(string or numeric value)
println() if we require a new line at the
end of the output
print() to stay on the current output line
Introduction to Object-Oriented Programming
1: Output
Slide 2
Printing values around strings
Suppose we wanted to print the values of two
variables, numCones (int) and unitPrice (double),
in a neat message
e.g., sold 5 cones at 2.50 each.
Using print and println,
System.out.print("sold " );
System.out.print( numCones );
System.out.print( " cones at " );
System.out.print( unitPrice );
System.out.println(" each." );
Introduction to Object-Oriented Programming
1: Output
Slide 3
String concatenation
Can use the + operator on strings
If both arguments are strings, it is simply
concatenated or “put together”, e.g.,
"Basket" + "ball" equals "Basketball"
"Hi, " + name equals "Hi, Bob"
assuming name has the value "Bob"
If one argument is a number, that argument is
converted to a string
"Hi, " + 5 equals "Hi, " + "5" = "Hi, 5"
"Price: " + unitPrice equals "Price: 2.50"
assuming price has the value 2.50
Introduction to Object-Oriented Programming
1: Output
Slide 4
Using concatenation with
print and println
Improved version
System.out.print("sold " + numCones );
System.out.println( " cones at " +
unitPrice + " each." );
Can actually be compressed into one
println statement
Note that the print or println method still
has one argument even in these cases
(a string)
Introduction to Object-Oriented Programming
1: Output
Slide 5
String formatting
Notice that particularly with floating point
numbers, we have no control over how many
spaces are occupied by a number when printed
e.g., 5 cones sold at 2.4999998 each
What would be preferable is to be able to
indicate up to how many decimal places are
allowed
Can be done in Java using the printf() method
Introduction to Object-Oriented Programming
1: Output
Slide 6
The printf() method
Even better version:
System.out.printf(
"sold %d cones at %.2f each.\n",
numCones, unitPrice );
\n means a newline,
same effect as
println()
prints: 5 cones sold at 2.50 each
More control:
System.out.printf(
"sold %3d cones at %6.2f each.\n",
numCones, unitPrice );
prints: ˍˍ5 cones sold at ˍˍ2.50 each
Introduction to Object-Oriented Programming
1: Output
Slide 7
System.out.printf()
// prints PI with 3 decimal places
// 6 spaces total: ˍ3.142 including the .
public class PrintingPI
{
public static void main( String args[] )
{
double pi = Math.PI;
System.out.printf("pi = %6.3f \n", pi);
}
Format specifier
}
Introduction to Object-Oriented Programming
1: Output
Slide 8
Format specifier
System.out.printf("pi = %6.3f \n", pi);
The % symbol signifies the start of the specifier
6 means that the width of the number is 6 characters
(including the . ; pad with leading spaces if needed)
The precision value 3 means that 3 decimal places are
required
The conversion character f means that the corresponding
argument is a floating-point number
\n results in a line break
Introduction to Object-Oriented Programming
1: Output
Slide 9
Conversion options
%10.3f : use f for float and double, first number
is the width, second number is the number of
decimal places
%5d : use d for integer types, the number
indicates the width
%10s : use s for strings, the number indicates the
width
%d %f %s with no numbers indicated means you
are leaving the formatting to Java defaults
Introduction to Object-Oriented Programming
1: Output
Slide 10