Simple Console Output What we will briefly discuss
Simple Console Output
What we will briefly discuss n n n System. out. println( … ) 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”) n Typing this in our first lab exercise resulted with the words Hello World displayed on the console n The words “Hello World” is a string and is used as a parameter to the method called println n More on strings, parameters and methods in our lessons next time
System. out. println( “Hello World”) n n 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) n Think of the console as the black MS-DOS window or the white output display box of Blue. J
What will be displayed? File: Revised. Hello. java // Revised Hello World application public class Revised. Hello { 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() n println() will display each string on a separate line Hello world Good Morning Java is cool
How about using print()? File: Revised. Hello. java // Revised Hello World application public class Revised. Hello { 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() n print() will NOT print the next string on a new line Hello world. Good Morning. Java is cool
System. out. print() n 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 Morning. Java is cool So is our teacher
Try this… File: Revised. Hello. java // Revised Hello World application public class Revised. Hello { 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” n 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: Advanced. Example. java // A demonstration of printf() public class Advanced. Example { 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 n n 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: Advanced. Example. java // A demonstration of printf() public class Advanced. Example { public static void main( String args[] ) { double pi = Math. PI; System. out. printf("pi = %5. 3 f n", pi); } }
System. out. printf() n The line System. out. printf("pi = %5. 3 f n", pi); will cause pi to be displayed as 3. 142
Format Specifiers n n Indicates how the arguments should be processed and where they should be inserted Syntax n n % - start of the specifier arg_index + '$' (optional) n n position of argument in the argument list flags (optional) n provides special formatting (e. g. left align, padding with zeroes)
Format Specifiers n Syntax (continued) n width (optional) n n n minimum number of spaces to print argument puts blanks in unused spaces '. ' + precision (optional) n number of digits after decimal conversion – how the argument should be formatted n. WE WILL DISCUSS THIS INS DETAIL IN OURFUTURE LESSONS ON STRINGS n Conversion Formatted into: d integers f floating-point number s String c character
The same concept simplified… System. out. printf("pi = %5. 3 f n", pi); n n n 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
- Slides: 19