Object Oriented Programming Using Java 2.3.3.1.9B Formatted Display
Total Page:16
File Type:pdf, Size:1020Kb
Object Oriented Programming Using Java 2.3.3.1.9B Formatted Display Formatted Display Using Printf and Format To print multiple argument with various formatting we use format() or printf() method • format /printf are printstream methods which means printing with formatting Syntax of printf(): System.out.printf(format, arguments); System.out.printf(locale, format, arguments); Syntax of format(): System.out.format(format, arguments); System.out.format(locale, format, arguments); • Locale specifies country name which effects date/time & number formats. A Locale object represents a specific geographical, political, or cultural region. It effects formats like numbers, currency, and percentages in a locale-sensitive manner Ex: Locale.US, Locale.UK, Locale.JAPAN • Format parameter specifies the formatting options like Data type, Width of display, Indentation, Sign , Precision Arguments are the data to be displayed as per the given formatting Format Specifier : To produces formatted output it requires a format string and an argument list. The format string contains fixed text and one or more embedded format specifiers. %[flags][width][.precision]conversion-character • Format string consists of literals and format specifier • format specifier consists of – % symbol starts the format rule – flags, width, precision as optional • Flags – modifies the output format • Width – Specifies width of display of the respective parameter • Precision – Specifies no. of digits of precision Conversion character specifies how to format the argument Conversion character: conversion is a character which specifies how the argument should be formatted. Basing on the data type the valid conversions for a given argument are specified c/C – formats character – Small c indicates to display character in small case and Capital C the character will be displayed in Caps s/S – formats strings –small s string will be displayed in lowercase and S displays in uppercase d – formats decimal integers o – formats octal integers- displays the octal equivalent of given integer x/X – formats hexa-decimal integers- displays the hexa decimal equivalent of given integer f/e – formats the floating-point numbers – f is for fixed point notation and e for exponential/scientific format t/T – formats date/time values- it is a prefix character, there will be a suffix character to implement various date/time formats b/B – formats Boolean- b displays true/false and B displays TRUE/FALSE Flags Flag Use - Left Justification + Add + sign to positive numbers ‘ ’ Adds a leading space to positive numbers ( Adds parenthesis to negative numbers) # Formats in alternate form 0 Zero padding , Locale specific grouping seperator < Reuse the previous argument In addition optionally we can provide and argument_index which is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc. For example if we have multiple formatting on same variable instead of repeating the same variable in the argument list, type it once and refer with argument index in format specifier. Formatting Characters public static void main(String[] args) { char c=‘a’"; System.out.printf("%n%c %C", c,c); } %c – Small/Lower case Character and %C- Upper Case Character Formatting Strings public static void main(String[] args) { String s1="java“,s2=“Programming”; System.out.printf("%n%s %S", s1,s1); System.out.printf("%n%8s'%-8s'%n%5.3s%3.5s",s1,s1,s2,s2 ); } %n is for new line %8s indicates the width of display of string as 8, if string have less than 8 characters remaining width is filled with space and right aligned %-8s same above except – indicates left alignment %5.3 total width of string display is 5 but only 3 characters of string will be displayed %3.5 total width of string display is 3 which is less than no. of characters to display it will display 5 characters Formatting Boolean public static void main(String[] args) { boolean c=true; System.out.printf("%b %B", c,c); System.out.printf(“%n%b %B", null,5);} Formatting Integers public static void main(String[] args) { int m1=99,m2=100,m3=98,m4=96; System.out.printf("M1=%dM2=%5d %+d",m1,m2,89 ); System.out.printf("%nM3=%-5dM4=%05d",m3,m4 ); } %5d indicates width of integer display is 5. %+d indicates whether the number is positive/negative the sign should be displayed. %-5d indicates width of integer display is 5 and – for left alignment. %05d indicates width of integer display is 5 and if number of digits is less than 5 then 0 is filled in free space. Formatting Floats public static void main(String[] args) { double n=-93.987,m= 89.5678; System.out.printf("%n%f %e", n,m); System.out.printf("%n'%+08.2f' '%-10.2e'", m,m); } %f- fixedpoint notation %e- scientific notation %+08.2f - fixedpoint notation, total width is 8 digits including sign and decimal point, 2 digit precision , sign is to be displayed for +ve and –ve numbers and fill free space with 0 %-10.2e- exponential notation, total width is 8 digits including sign and decimal point, 2 digit precision and left aligned Formatting Date & Time : suffix character is added to %t to implement various date/time formats. %tD Date %td 2digit date %tT Time %tm 2digit month %tH Hours %ty 2 digit year %tM Minutes %tY 4 digit year %tS Seconds %tB full name of Month %tL Milliseconds %tA full day of Week %tN Nanoseconds %tl hour in 12 hour format %tp am/pm %tz Timezone offset import java.util.date; public static void main(String[] args) { Date date=new Date(); System.out.printf("%n%tD %tT",date,date); System.out.printf("%n%td %tB %tY",date,date,date); System.out.printf("%n%1$tH:%1$tM:%1$tS-%1$tL,%1$tN %1$tp ",date); } 1$ indicates the positional argument indicating date variable we don’t use positional argument the statement is to be written as System.out.printf("%n%1$tH:%1$tM:%1$tS-%1$tL,%1$tN %1$tp ",date,date,date,date,date,date); All the format specifiers used with printf can be used with format without any change as both have same syntax. Using above examples with Format() method import java.util.Date; public class FormatDemo { public static void main(String[] args) { int m1=99,m2=100,m3=98,m4=96; System.out.format("M1=%dM2=%5d%+d",m1,m2,87 ); System.out.format("%nM3=%-5dM2=%05d",m3,m4 ); boolean c=true; System.out.printf("%b %B", c,c); System.out.format("%n%b %B", null,5); String s1="java",s2="Programming"; System.out.format("%n%s %S", s1,s1); System.out.format("%n%8s'%-8s'%n%5.3s%3.5s",s1,s1,s2,s2 ); double n=-93.987,m=89.5678; System.out.format("%n%f %e", n,m); System.out.format("%n'%+08.2f' '%-10.2e'", m,m); Date date=new Date(); System.out.format("%n%tD %tT",date,date); System.out.format("%n%td %tB %tY",date,date,date); System.out.format("%n%1$tH:%1$tM:%1$tS-%1$tL,%1$tN %1$tp ",date); } Formatting Using String format method String format(locale, format, arguments) method formats the arguments as per the locale and format specifier and return in the form of string. format() method of String class is similar to format() and printf() methods of PrintStream class, the only difference is format() method of string class returns formatted string which we can later display using System.out where as format() and printf() methods of PrintStream class directly displays formatted values on to the screen. Syntax: public static String format(String format, Object... args) public static String format(Locale localeString, format, Object... args) Locale specifies country name which effects date/time & number formats Format string consists of literals and format specifier which starts with % specifies formatting options like Data type(conversion character), flags like alignment, Sign Width of display, Precision Arguments are the data to be displayed as per the formatting Ex: public class StringFormatDemo { public static void main(String[] args) { int m1=99,m2=100,m3=98,m4=96; String s1=String.format("M1=%dM2=%5d%+d",m1,m2,87 ); String s2=String.format("%nM3=%-5dM2=%05d",m3,m4 ); System.out.print(s1); System.out.print(s2); boolean c=true; String s3=String.format("%b %B", c,c); String s4=String.format("%n%b %B", null,5); System.out.print(s3+s4); } } Keypoints • printf() and format() both are PrintStream methods used for formatting • printf() and format() both have same syntax and can be used interchangeably. • format() method of String class is similar to format() and printf() methods of PrintStream class, the only difference is format() method of string class returns formatted string which we can later display using System.out where as format() and printf() methods of PrintStream class directly displays formatted values on to the screen. Web References https://www.oercommons.org/courses/java-tutorial/view https://nptel.ac.in/courses/106/105/106105191/ Lecture 30 : I-O Stream-I https://https://docs.oracle.com/javase/tutorial/java/data/numberformat.html Book Reference Programming with JAVA, E Balaguruswamy Java,Java,Java Object Oriented Problem Solving, R Morelli and R Walde .