More lO.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

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.
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.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 Miscellaneous Java … a collection of short topics that we need to get to at some point …
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
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.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
 Pearson Education, Inc. All rights reserved Formatted Output.
 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.
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
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.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
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.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CompSci 230 S Programming Techniques
28 Formatted Output.
Input/Output.
Console Input and Output
Formatting Output & Enumerated Types & Wrapper Classes
Formatted Output (printf)
Yanal Alahmad Java Workshop Yanal Alahmad
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.
CMSC 202 Static Methods.
Formatted Input/Output
Formatting Output.
System.out.println for console output
API, Wrapper classes, printf, and methods
Lecture 13 Input/Output Files.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Chapter 3: Introduction to Objects and Input/Output
Programming Assignment #1 12-Month Calendar—
Formatted Input/Output
Conversion Check your class notes and given examples at class.
Introduction to Java Applications
Classes and Objects Static Methods
Formatted Input/Output
CS 1054 Introduction to Programming in Java
Input and Formatted Output (printf)
Classes and Objects Object Creation
Putting Values into a Suitable Form for Display
CMSC 202 Constructors Version 9/10.
Presentation transcript:

More lO

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 © 2006 Pearson Addison-Wesley. All rights reserved

Formatting Output with printf 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 © 2006 Pearson Addison-Wesley. All rights reserved

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) © 2006 Pearson Addison-Wesley. All rights reserved

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 © 2006 Pearson Addison-Wesley. All rights reserved

Line Breaks with printf Line breaks can be included in a format string using %n The code double price = 19.8; String name = "magic apple"; System.outprintf("$%6.2f for each %s.%n", price, name); System.out.println("Wow"); will output $ 19.80 for each magic apple. Wow © 2006 Pearson Addison-Wesley. All rights reserved

Format Specifiers for System.out.printf © 2006 Pearson Addison-Wesley. All rights reserved

The printf Method (Part 1 of 3) © 2006 Pearson Addison-Wesley. All rights reserved

The printf Method (Part 2 of 3) %f is equivalent to %.6f © 2006 Pearson Addison-Wesley. All rights reserved

The printf Method (Part 3 of 3) © 2006 Pearson Addison-Wesley. All rights reserved

Formatting Money Amounts with printf A good format specifier for outputting an amount of money stored as a double type is %.2f It says to include exactly two digits after the decimal point and to use the smallest field width that the value will fit into: double price = 19.99; System.out.printf("The price is $%.2f each.") produces the output: The price is $19.99 each. © 2006 Pearson Addison-Wesley. All rights reserved

Legacy Code Code that is "old fashioned" but too expensive to replace is called legacy code Sometimes legacy code is translated into a more modern language The Java method printf is just like a C language function of the same name This was done intentionally to make it easier to translate C code into Java © 2006 Pearson Addison-Wesley. All rights reserved

Money Formats Using the NumberFormat class enables a program to output amounts of money using the appropriate format The NumberFormat class must first be imported in order to use it import java.text.NumberFormat An object of NumberFormat must then be created using the getCurrencyInstance() method The format method takes a floating-point number as an argument and returns a String value representation of the number in the local currency © 2006 Pearson Addison-Wesley. All rights reserved

Money Formats import java.text.NumberFormat; public class CurrencyFormatDemo { public static void main(String[] args) System.out.println("Default location:"); NumberFormat moneyFormater = NumberFormat.getCurrencyInstance(); System.out.println(moneyFormater.format(19.8)); System.out.println(moneyFormater.format(19.81111)); System.out.println(moneyFormater.format(19.89999)); System.out.println(moneyFormater.format(19)); System.out.println(); } © 2006 Pearson Addison-Wesley. All rights reserved

Money Formats Output of the previous program Default location: $19.80 $19.81 $19.90 $19.00 © 2006 Pearson Addison-Wesley. All rights reserved

Specifying Locale Invoking the getCurrencyInstance() method without any arguments produces an object that will format numbers according to the default location In contrast, the location can be explicitly specified by providing a location from the Locale class as an argument to the getCurrencyInstance() method When doing so, the Locale class must first be imported import java.util.Locale; © 2006 Pearson Addison-Wesley. All rights reserved

Specifiying Locale import java.text.NumberFormat; import java.util.Locale; public class CurrencyFormatDemo { public static void main(String[] args) System.out.println("US as location:"); NumberFormat moneyFormater2 = NumberFormat.getCurrencyInstance(Locale.US); System.out.println(moneyFormater2.format(19.8)); System.out.println(moneyFormater2.format(19.81111)); System.out.println(moneyFormater2.format(19.89999)); System.out.println(moneyFormater2.format(19)); } © 2006 Pearson Addison-Wesley. All rights reserved

Specifying Locale Output of the previous program US as location: $19.80 $19.81 $19.90 $19.00 © 2006 Pearson Addison-Wesley. All rights reserved

Locale Constants for Currencies of Different Countries © 2006 Pearson Addison-Wesley. All rights reserved

import java.text.NumberFormat; import java.util.Locale; public class CurrencyFormatDemo { public static void main(String[] args) NumberFormat formater1 = NumberFormat.getCurrencyInstance(Locale.US); NumberFormat formater2 = NumberFormat.getCurrencyInstance(Locale.CHINA); NumberFormat formater3 = NumberFormat.getCurrencyInstance(Locale.JAPAN); NumberFormat formater4 = NumberFormat.getCurrencyInstance(Locale.UK); NumberFormat formater5 = NumberFormat.getCurrencyInstance(Locale.FRANCE); System.out.println( "\nMoney: Locale is US" ); System.out.println(formater1.format(19.8)); System.out.println( "\nMoney: Locale is CHINA" ); System.out.println(formater2.format(19.8)); System.out.println( "\nMoney: Locale is JAPAN" ); System.out.println(formater3.format(19.8)); System.out.println( "\nMoney: Locale is UK" ); System.out.println(formater4.format(19.8)); System.out.println( "\nMoney: Locale is FRANCE" ); System.out.println(formater5.format(19.8)); } © 2006 Pearson Addison-Wesley. All rights reserved

Money: Locale is FRANCE 19,80 € Money: Locale is US $19.80 Money: Locale is CHINA CNY19.80 Money: Locale is JAPAN JPY20 Money: Locale is UK £19.80 Money: Locale is FRANCE 19,80 € © 2006 Pearson Addison-Wesley. All rights reserved

Importing Packages and Classes Libraries in Java are called packages A package is a collection of classes that is stored in a manner that makes it easily accessible to any program In order to use a class that belongs to a package, the class must be brought into a program using an import statement Classes found in the package java.lang are imported automatically into every Java program import java.text.NumberFormat; // import theNumberFormat class only import java.text.*; //import all the classes in package java.text © 2006 Pearson Addison-Wesley. All rights reserved