







































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Console Input Using the Scanner Class. Starting with version 5.0, Java includes a class for doing simple keyboard input named the Scanner.
Typology: Exercises
1 / 47
This page cannot be seen from the preview
Don't miss anything!








































Adapted from Absolute Java, Rose Williams, Binghamton University
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
double price = 19.8; String name = "magic apple";
System.out.println("Wow");
$ 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
(^) 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
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