ࡱ> {}ze Bbjbj F]di\di\2TTTTThhh8$|h_6"""_______$a{d4_T #"" # #4_TTI_t)t)t) #TT_t) #_t)t)rX6]0_)Q$l[F^__0_d[d&d6]6]dTJ]""t)" "Y"""4_4_'"""_ # # # #d"""""""""> : Computer Programming I Instructor: Greg Shaw COP 2210 PRIVATE  Using printf to Format Output I. Unformatted (Default) Output vs. Formatted Output We are all familiar with the default or unformatted appearance of Java output, as produced by the print and println methods The field width is the minimum necessary to display all the characters there are no blanks printed before or after The number of decimal places shown is however many are stored in the variable or the result of the calculation the output is not rounded to a convenient number of digits When we format the output, we can right- or left-justify a value in a field of a specified width, and round floating-point numbers to a specified number of digits This document explains how to format output by calling the printf method of the PrintStream class (recall that System.out is an object of the PrintStream class) II. printf Syntax System.out.printf(format-string, expr1, expr2, ... , exprn) ; The format-string is a string expression containing format specifiers which determine how each of the following expressions will be printed expri is a variable or expression to be formatted and printed Any number of expressions may follow the format-string, which must contain one format specifier for each expression In addition to the format specifiers, the format string may contain string literals, which will be printed exactly as they appear III. Format Specifiers %wd for integer types %ws for strings %wf for floating-point types %n - newline where w is an integer literal that specifies the width of the output field The corresponding expression will be printed right-justified in a field of w spaces (i.e., any extra spaces will appear to the left of the expression) If the specified field width is too small to display the expression, it will be ignored and the expression will be printed using the minimum field width needed To specify the minimum field width, just omit the width specifier w (see examples below) IV. String and int Formatting Examples Given the variable values below, note the output from each of the following statements (extra spaces printed are shown as ^). int number = 37 ; int number2 = 73 ; String name = "Dave" ; System.out.printf("Your lucky number is %d", number) ; Your lucky number is 37 (note minimum field width) System.out.printf("Your lucky number is %6d", number) ; Your lucky number is ^^^^37 (right-justified in field of 6) System.out.printf("Im sorry %s,%nIm afraid I cant do that.", name) ; Im sorry Dave, Im afraid I cant do that. (note newline specifier, %n) System.out.printf("Answers are %5d and %7d.", number, number2) ; Answers are ^^^37 and ^^^^^73. V. Floating-Point Formatting Examples When formatting a floating-point expression, you have the additional option of specifying the number of decimal places to be displayed The format specifier used has this syntax: %w.df where w is the width of the output field and d is the number of digits to the right of the decimal point The expression will be rounded to the specified number of decimal places If the expression has fewer than the specified number of digits to the right of the decimal point, it will be right-extended (i.e. padded) with zeroes double x = -12.345 ; System.out.printf("%10.2f",x) ; // ^^^^-12.35 (rounds up) System.out.printf("%10.1f",x) ; // ^^^^^-12.3 (rounds down) System.out.printf("%12.5f",x) , // ^^^-12.34500 (padded with 0s) VI. Format Flags For most applications the format specifiers alone will suffice For more formatting options, format flags may be added Format flags are single characters which go immediately after the % in the format specifier The format flags are summarized in this table: FlagEffect - (minus sign) left-alignment , (comma) use comma separators in numeric expressions ( (left parenthesis) enclose negative numbers in parentheses + (plus sign) show a plus sign for positive numbers 0 (zero) print leading zeroes instead of spaces String name = "Bubba" ; int num = 1000000 ; double trouble = -123.4567 ; System.out.printf("Name: %-12s",name) ; // Name: Bubba^^^^^^^ System.out.printf("That's the $%,d question!", num) ; Thats the $1,000,000 question! System.out.printf("%(10.2f", trouble) ; // ^^(123.46) VII. String Class Method format The String class has a static method named format that works exactly like printf except that it returns a formatted string instead of printing one. So you can use String.format in the argument to print or println. For examples, see the main method of the PayMaster1 class in Unit 5 and the getdata method of the PayCheck2 class (in Paymaster2.java, also in Unit 5) VIII. Using Variables in Format Strings We might want to use a variable in a format string so that the width of the output field can be changed by changing the value of the variable. To do this, simply use concatenation to create the format string. E.g. to right-justify the string name in a field of exactly width spaces, use System.out.printf("%" + width + "s", name) ; or System.out.println( String.format("%" + width + "s", name) ) ; So, if name is Bubba and int variable width is set to 10, the output of each of these statements would be ^^^^^Bubba (where each ^ represents a space)   IJRSTW]ctuvwzǶ⯧wnweYM=h9oB*OJQJ^JaJphhrOJQJ^JaJh07hr6CJaJh86CJaJh}6CJaJh6CJaJh07h6CJaJh)OJQJ^JaJhOJQJ h8hh8h86 h8h8!jh@OJQJUh@OJQJjh@OJQJUh@OJQJh8@OJQJh- @OJQJ4UVWuvw+ , P Q $ 7$8$H$^`a$gdi$ & F 7$8$H$^`a$gdi$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ό|qcXcXKchHhHCJ^JaJhH6CJ^JaJh)hH6CJ^JaJh.6CJ^JaJhlQB*OJQJ^JaJphhHB*OJQJ^JaJphhZ%_B*OJQJ^JaJph%h:h:B*OJQJ^JaJphhVB*OJQJ^JaJphh:B*OJQJ^JaJphh@B*OJQJ^JaJphhB*OJQJ^JaJph:;<]^67LMPQ h7$8$H$^hgdbE h7$8$H$^hgdbE$a$gdbE$a$gdH h7$8$H$^hgdL^ #(,357CDMQ`jq{빧˔˔˧j˔(h.h.6B*OJQJ^JaJph(h.hH6B*OJQJ^JaJph%h.hHB*OJQJ^JaJph"h.6B*OJQJ^JaJph"hH6B*OJQJ^JaJphh.B*OJQJ^JaJphhHB*OJQJ^JaJph(hHhH6B*OJQJ^JaJph$ 02356AB￷h\ jh\ U%h.hHB*OJQJ^JaJph"hbE6B*OJQJ^JaJphhbE6CJ^JaJhbEB*OJQJ^JaJph  0245789:<=?@ABd h7$8$H$^hgdL^ h7$8$H$^hgdbED....()()))()00P1F8$:p.BP/ =!"#$%8 DpTD phoenixQG$$Ifh!vh#v #v:V lL t0d&6,5 5ahytJ$$Ifh!vh#v #v:V l t0d&65 5ahytJ$$Ifh!vh#v #v:V l t0d&65 5ahytJ$$Ifh!vh#v #v:V l t0d&65 5ahytJ$$Ifh!vh#v #v:V l t0d&65 5ahytJ$$Ifh!vh#v #v:V l t0d&65 5ahytJ*s666666666vvvvvvvvv66666686666666666666666666666666666666666666666666666666hH6666666666666666666666666666666666666666666666666666666666666666662 0@P`p2( 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p8XV~ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@_HmH nH sH tH H`H Normal1$CJOJQJ_HmH sH tH P@P ) Heading 1$$*$@&a$56CJ OJQJll  Heading 2,$$ & F h8p*$@&a$5@CJOJQJDA D Default Paragraph FontVi@V  Table Normal :V 44 la (k (No List 4+@4  Endnote Text>* > Endnote ReferenceH*66  Footnote Text@& !@ Footnote ReferenceH*NN TOC 1) $ 0*$]^`0JJ TOC 2% $ 0*$]^`0JJ TOC 3% $ p0*$]^p`0JJ TOC 4% $ @ 0*$]^@ `0JJ TOC 5% $ 0*$]^`0BB TOC 6 $0*$^`0:: TOC 70*$^`0BB TOC 8 $0*$^`0BB TOC 9 $ 0*$^`0N N Index 1% $ `*$]^``N N Index 2% $ 0*$]^`0>.>  TOA Heading *$ $*"* Caption:/: _Equation Caption@B@ Body Text !$*$a$OJQJ@Y"@  Document Map"-D OJQJ|C2| Body Text Indent2#$ h8p0*$^`0a$@CJOJQJxRBx Body Text Indent 2*$$ h8p*$^a$@CJOJQJbPRb Body Text 2"%$ h8p*$a$@CJOJQJXSbX Body Text Indent 3 &^6CJOJQJHrH  Balloon Text'CJOJQJ^JaJjj M9 Table Grid7:V(0(F/F bEHeading 1 Char56CJ OJQJPK![Content_Types].xmlN0EH-J@%ǎǢ|ș$زULTB l,3;rØJB+$G]7O٭VvnB`2ǃ,!"E3p#9GQd; H xuv 0F[,F᚜K sO'3w #vfSVbsؠyX p5veuw 1z@ l,i!b I jZ2|9L$Z15xl.(zm${d:\@'23œln$^-@^i?D&|#td!6lġB"&63yy@t!HjpU*yeXry3~{s:FXI O5Y[Y!}S˪.7bd|n]671. tn/w/+[t6}PsںsL. J;̊iN $AI)t2 Lmx:(}\-i*xQCJuWl'QyI@ھ m2DBAR4 w¢naQ`ԲɁ W=0#xBdT/.3-F>bYL%׭˓KK 6HhfPQ=h)GBms]_Ԡ'CZѨys v@c])h7Jهic?FS.NP$ e&\Ӏ+I "'%QÕ@c![paAV.9Hd<ӮHVX*%A{Yr Aբ pxSL9":3U5U NC(p%u@;[d`4)]t#9M4W=P5*f̰lk<_X-C wT%Ժ}B% Y,] A̠&oʰŨ; \lc`|,bUvPK! ѐ'theme/theme/_rels/themeManager.xml.relsM 0wooӺ&݈Э5 6?$Q ,.aic21h:qm@RN;d`o7gK(M&$R(.1r'JЊT8V"AȻHu}|$b{P8g/]QAsم(#L[PK-![Content_Types].xmlPK-!֧6 0_rels/.relsPK-!kytheme/theme/themeManager.xmlPK-!R%theme/theme/theme1.xmlPK-! ѐ' theme/theme/_rels/themeManager.xml.relsPK] B\  { g5;Hf"B ")+, ;=v :B!#$%&'(*-@ 0e0e     @  1 8c8c     ?1 d0u0@Ty2 NP'p<'pA)BCD|E||@0(  B S  ?4& 4C7C?*urn:schemas-microsoft-com:office:smarttags stockticker EPw| f w  6 G r l},30Rdfs245789:<=?C+0 f x l p & 6 H r 69:=NTl~1MORe2C3333333333333333333333333333uu**    N Y : H I O P EEGJ^u::==:;122489:;<=>?Cuu**    N Y : H I O P EEGJ^u::==:;122489:<=?C )8pic\~ ,>? Ħ(89T֘ew5b}Zg+l&>$c c(T֘e-U,XO7ݎz_7p8&2d(HT֘eqH+L]mOl/- Q)8eZ&@[nBGdT֘e9hT֘e!$D3h\ lQCyN Y^Ivn:?J)$t?bv)M9hnEb.r)KA#(@j9oY5~l+zkav%0^!4 - 24@B@Unknown G.[x Times New Roman5Symbol3. .[x Arial?= .Cx Courier New71 Courier5. .[`)TahomaSMonotype SortsSymbol;WingdingsA$BCambria Math"9)x`Zai | )  &)  &)Px4(( 2Q)PHP?^2!xx Introduction to Programming M.C. Shaw Greg Shaw                           Oh+'0  8 D P \hpxIntroduction to Programming M.C. ShawNormal Greg Shaw3Microsoft Office Word@vA@,?-@C@)Q)  ՜.+,0 hp  Hotel Degregorio& ( Introduction to Programming Title  !"#$%&'()*+,-.012345689:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghiklmnopqstuvwxy|Root Entry F0)Q~Data /1Table7eWordDocumentF]SummaryInformation(jDocumentSummaryInformation8rCompObjr  F Microsoft Word 97-2003 Document MSWordDocWord.Document.89q