Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Simple Console Output CS 21a

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

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

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

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

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

7 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

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

9 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

10 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

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

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

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

14 Format Specifiers Indicates how the arguments should be processed and where they should be inserted Syntax % - start of the specifier width (optional) minimum number of spaces to print argument puts blanks in unused spaces '.' + precision (optional) number of digits after decimal Conversion character Conversion Formatted into: dintegers ffloating-point number sString ccharacter

15 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


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

Similar presentations


Ads by Google