2 InputOutputFormatting
2 InputOutputFormatting
Introduction to Programming
Chapter 2 - Console Input & Output
Dr. Aiman Hanna
Department of Computer Science & Software Engineering
Concordia University, Montreal, Canada
These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch;
which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by
Pearson Education / Addison-Wesley.
2-2
println Versus print
Another method that can be invoked by the
System.out object is print
2-3
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
2-5
Right and Left Justification in printf
2-8
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
Starting with version 5.0, Java includes a class for doing simple
keyboard input named the Scanner class
2-10
Console Input Using the Scanner Class
2-11
Pitfall: Dealing with the Line Terminator,
'\n'
The method nextLine of the class Scanner reads the remainder
of a line of text starting wherever the last keyboard reading left off
Java does not enforce it; so you will only get a warning if
you do not close an opened Scanner object (or objects if
you create more than one)
Nonetheless, you should close it/them before your end
your program
2-14
Methods in the Class Scanner
(Part 1 of 3)
2-15
Methods in the Class Scanner
(Part 2 of 3)
2-16
Methods in the Class Scanner
(Part 3 of 3)
2-17
Other Input Delimiters
The delimiters that separate keyboard input can be changed
when using the Scanner class
2-19
Changing the Input Delimiter
(Part 2 of 3)
2-20
Changing the Input Delimiter
(Part 3 of 3)
2-21
Money Formats
Using the NumberFormat class enables a program to output
amounts of money using the appropriate format
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();
}
}
2-23
Money Formats
Output of the previous program
Default location:
$19.80
$19.81
$19.90
$19.00
2-24
Specifying Locale
Invoking the getCurrencyInstance() method
without any arguments produces an object that will
format numbers according to the default location
2-25
Specifying Locale
import java.text.NumberFormat;
import java.util.Locale;
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));
}
}
2-26
Specifying Locale
Output of the previous program
US as location:
$19.80
$19.81
$19.90
$19.00
2-27
Locale Constants for Currencies of Different
Countries
2-28
The DecimalFormat Class
Using the DecimalFormat class enables a program to format
numbers in a variety of ways
The DecimalFormat class must first be imported
2-30
The DecimalFormat Class
(Part 2 of 3)
2-31
The DecimalFormat Class
(Part 3 of 3)
2-32