Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )

Similar presentations


Presentation on theme: "Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )"— Presentation transcript:

1 Simple Console Output

2 What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )

3 Remember our Lab 0 File: Hello.java // Hello World application public class Hello { public static void main( String args[] ) { System.out.println( “Hello world” ); }

4 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

5 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

6 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” ); }

7 System.out.println() println() will display each string on a separate line Hello world Good Morning Java is cool

8 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” ); }

9 System.out.print() print() will NOT print the next string on a new line Hello worldGood MorningJava is cool

10 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

11 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” ); }

12 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

13 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

14 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?

15 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); }

16 System.out.printf() The line System.out.printf("pi = %5.3f \n", pi); will cause pi to be displayed as 3.142

17 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)

18 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 Formatted into: dintegers f floating-point number sString ccharacter conversion – how the argument should be formatted WE WILL DISCUSS THIS INS DETAIL IN OURFUTURE LESSONS ON STRINGS

19 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


Download ppt "Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )"

Similar presentations


Ads by Google