Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Simple Console Output
CS 21a
What we will briefly discuss
System.out.println( … )
System.out.print( … )
System.out.printf( … )
Consider the following program
File: RevisedHello.java
// Revised Hello World application
public class RevisedHello
{
public static void main( String args[] )
{
System.out.println( “Hello world” );
System.out.println( “Good morning” );
System.out.println( “Java is cool” );
}
}
System.out.println()
println() will display each string on a
separate line
Hello world
Good Morning
Java is cool
How about using print()?
File: RevisedHello.java
// Revised Hello World application
public class RevisedHello
{
public static void main( String args[] )
{
System.out.print( “Hello world” );
System.out.print( “Good morning” );
System.out.print( “Java is cool” );
}
}
System.out.print()
print() will NOT print the next string on
a new line
Hello worldGood MorningJava is cool
System.out.print()
Another example
System.out.println( “Hello world” );
System.out.print( “Good morning” );
System.out.println( “Java is cool”);
System.out.println( “So is our teacher” );
Hello world
Good MorningJava is cool
So is our teacher
Try this…
File: RevisedHello.java
// Revised Hello World application
public class RevisedHello
{
public static void main( String args[] )
{
System.out.print( “Hello world” + “\n” );
System.out.print( “Good morning” + “\n” );
System.out.print( “Java is cool” + “\n” );
}
}
Using print() with “\n”
Even if print() does not display
consecutive strings in separate lines,
the “\n” character causes a line break
The same effect of println() can be
achieved
Now try this…
File: AdvancedExample.java
// A demonstration of printf()
public class AdvancedExample
{
public static void main( String args[] )
{
double pi = Math.PI;
System.out.println( pi );
}
}
//note: pi is a variable assigned to the value of pi.
//Math.PI is a constant value from Math.java
//More on this in our future lessons
Formatting numbers
The code will display
3.141592653589793 on the console
What if we want to force pi to be
displayed with up to just 3 decimal
places?
System.out.printf()
File: AdvancedExample.java
// A demonstration of printf()
public class AdvancedExample
{
public static void main( String args[] )
{
double pi = Math.PI;
System.out.printf("pi = %5.3f \n", pi);
}
}
System.out.printf()
The line
System.out.printf("pi = %5.3f \n", pi);
will cause pi to be displayed as 3.142
Format Specifiers
Indicates how the arguments
should be processed and
where they should be
inserted
Syntax
% - start of the specifier
width (optional)
'.' + precision (optional)
minimum number of
spaces to print argument
puts blanks in unused
spaces
number of digits after
decimal
Conversion character
Conversion
Formatted into:
d
integers
f
floating-point number
s
String
c
character
Summary of printf example
System.out.printf("pi = %5.3f \n", pi);
The % symbol signifies the start of the
specifier
5 means that the width value of the number
is 5 characters
The precision value 3 means that 3 decimal
places are required
The conversion character f means that the
resulting pi is a floating-point number
\n results in a line break