/*** Book: Thinking in Java * Features: Formatting parameters for the Java string format specifier * format string syntax as follows: *%[argument_index$][flags][width][.precision] conversion* file: receipt.java* time: April 11, 2015 19:40:53* Author: cutter_point*/package Lesson13_strings;import Java.util.formatter;public class Receipt {private Double total = 0;private Formatter f = new Formatter (system.out);//Specifies the output Destination public void PrintTitle ()//Output Caption {//This format the first%-15s is said to be a width of 15 string, followed by similar, just-still not very clear what is a F.format ("%-15s%5s%10s\n", "Item", " Qty "," price "), F.format ("%-15s%5s%10s\n ","----","---","-----");} public void print (String name, Int. qty, double price) {F.format ("%-15.15s%5d%10.2f\n", Name, qty, price); public void Printtotal () {F.format ("%-15.15s%5s%10.2f\n", "tax", "", total*0.06); F.format ("%-15s%5s%10s\n", "", "", "-- ----"); F.format ("%-15.15s%5s%10.2f\n "," Total "," ", total*1.06);} public static void Main (String [] args) {Receipt Receipt = new Receipt (); Receipt.printtitle (); Receipt.print ("Jack ' s Magic Beans ", 4, 4.25); Receipt.print ("PrincessPeas ", 3, 5.1); Receipt.print ("Three Bears porridge", 1, 14.29); Receipt.printtotal (); Formatter ff = new Formatter (System.out); Ff.format ("%5d", 998);}}
Output:
item Qty price
---- --- -----
Jack ' s Magic be 4 4.25
Princess peas 3 5.10
Three Bears por 1 14.29
tax 1.42
------
total 25.06
998
"Thinkinginjava" 26, Java string format specifier