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
What we will briefly discuss
System.out.println( … )
System.out.print( … )
System.out.printf( … )
Remember our Lab 0
File: Hello.java
// Hello World application
public class Hello
{
public static void main( String args[] )
{
System.out.println( “Hello world” );
}
}
System.out.println( “Hello World”)
Typing this in our first lab exercise resulted
with the words Hello World displayed on the
console
The words “Hello World” is a string and is
used as a parameter to the method called
println
More on strings, parameters and methods in our
lessons next time
System.out.println( “Hello World”)
For now, we know that whatever we type
within the quotation marks in
System.out.println( … ) will be displayed
on the console
The console refers to our display screen void
of any graphics or graphical user interface
(GUI) objects (ex. Buttons)
Think of the console as the black MS-DOS window
or the white output display box of BlueJ
What will be displayed?
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
arg_index + '$' (optional)
position of argument in the argument list
flags (optional)
provides special formatting (e.g. left align, padding with
zeroes)
Format Specifiers
Syntax (continued)
width (optional)
minimum number of spaces to print argument
puts blanks in unused spaces
'.' + precision (optional)
number of digits after decimal
conversion – how the
argument should be
formatted
WE WILL DISCUSS THIS INS
DETAIL IN OURFUTURE
LESSONS ON STRINGS
Conversion
Formatted into:
d
integers
f
floating-point
number
s
String
c
character
The same concept simplified…
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