System.out.println for console output

Slides:



Advertisements
Similar presentations
Formatting Output For a pleasant interaction with the user.
Advertisements

Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
CIS 270—Application Development II Chapter 28—Formatted Output.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
 Pearson Education, Inc. All rights reserved Formatted Output.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Formatting Screen Output How do I make my numbers look pretty?
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
CompSci 230 S Programming Techniques
28 Formatted Output.
C Formatted Input/Output
Chapter 1 – Introduction
More lO.
Input/Output.
Console Input and Output
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Formatting Screen Output How do I make my numbers look pretty?
Chapter 2 Screen Output Section 2.1
Console Input and Output
OUTPUT STATEMENTS GC 201.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Formatting Output.
Lecture 13 Input/Output Files.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Programming Assignment #1 12-Month Calendar—
Conversion Check your class notes and given examples at class.
Introduction to Java Applications
The keyboard is the standard input device.
CS 1054 Introduction to Programming in Java
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

System.out.println for console output System.out is an object that is part of the Java language println is a method invoked by the System.out object that can be used for console output The data to be output is given as an argument in parentheses A plus sign is used to connect more than one item Every invocation of println ends a line of output System.out.println("The answer is " + 42);

Formatting Output with printf Starting with version 5.0, Java includes a method named printf that can be used to produce output in a specific format The Java method printf is similar to the print method Like print, printf does not advance the output to the next line System.out.printf can have any number of arguments The first argument is always a format string that contains one or more format specifiers for the remaining arguments All the arguments except the first are values to be output to the screen

printf Format Specifier The code double price = 19.8; System.out.print("$"); System.out.printf("%6.2f", price); System.out.println(" each"); will output the line $ 19.80 each The format string "%6.2f" indicates the following: End any text to be output and start the format specifier (%) Display up to 6 right-justified characters, pad fewer than six characters on the left with blank spaces (i.e., field width is 6) Display exactly 2 digits after the decimal point (.2) Display a floating point number, and end the format specifier (i.e., the conversion character is f)

Right and Left Justification in printf The code double value = 12.123; System.out.printf("Start%8.2fEnd", value); System.out.println(); System.out.printf("Start%-8.2fEnd", value); will output the following Start 12.12End Start12.12 End The format string "Start%8.2fEnd" produces output that is right justified with three blank spaces before the 12.12 The format string "Start%-8.2fEnd" produces output that is left justified with three blank spaces after the 12.12

Multiple arguments with printf The following code contains a printf statement having three arguments The code double price = 19.8; String name = "magic apple"; System.out.printf("$%6.2f for each %s.", price, name); System.out.println(); System.out.println("Wow"); will output $ 19.80 for each magic apple. Wow Note that the first argument is a format string containing two format specifiers (%6.2f and %s) These format specifiers match up with the two arguments that follow (price and name)

Format Specifiers for System.out.printf