Docsity

Console Input and Output, Exercises of Java Programming

Partial preview of the text

Download Console Input and Output and more Exercises Java Programming in PDF only on Docsity!

Adapted from Absolute Java, Rose Williams, Binghamton University

Module 3

Console Input and Output

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)

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

Format Specifiers for System.out.printf

The printf Method (2/3)

The printf Method (3/3)

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

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

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;

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

Locale Constants for Currencies of Different Countries

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  (^) A DecimalFormat object is associated with a pattern when it is created using the new command  (^) The object can then be used with the method format to create strings that satisfy the format  (^) An object of the class DecimalFormat has a number of different methods that can be used to produce numeral strings in various formats