Formatting text with printf System out printfformat string

  • Slides: 11
Download presentation
Formatting text with printf System. out. printf("format string", parameters); • A format string can

Formatting text with printf System. out. printf("format string", parameters); • A format string can contain placeholders to insert parameters: integer real number string %d %f %s • these placeholders are used instead of + concatenation – Example: Second %d specifies how to print the second parameter First %d goes specifies how to print the first parameter int x = 3; int y = -17; System. out. printf("x is %d and y is %d!n", x, y); // x is 3 and y is -17! • printf does not drop to the next line unless you write n 1

printf width • • %Wd %-Wd %Wf. . . integer, W characters wide, right-aligned

printf width • • %Wd %-Wd %Wf. . . integer, W characters wide, right-aligned integer, W characters wide, left-aligned real number, W characters wide, right-aligned for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 10; j++) { System. out. printf("%4 d", (i * j)); } System. out. println(); // to end the line } Output: (nice columns due to W, value 4) 1 2 3 4 5 6 7 2 4 6 8 10 12 14 3 6 9 12 15 18 21 8 16 24 9 18 27 10 20 230

printf precision • %. Df real number, rounded to D digits after decimal •

printf precision • %. Df real number, rounded to D digits after decimal • %W. Df real number, W chars wide, D digits after decimal • %-W. Df real number, W wide (left-align), D after decimal double gpa = 3. 253764; System. out. printf("your GPA is %. 1 fn", gpa); System. out. printf("more precisely: %8. 3 fn", gpa); Output: your GPA is 3. 3 more precisely: 3 3. 254 8 3

printf question • Write a Receipt method to nicely format its output. – Display

printf question • Write a Receipt method to nicely format its output. – Display the subtotal, tax, tip, and total as shown in the format below, with $ and 2 digits after. • Example log of execution: How many people ate? 4 Person #1: How much did Person #2: How much did Person #3: How much did Person #4: How much did Subtotal: Tax: Tip: Total: your dinner cost? 20. 00 15 25. 0 10. 00 $70. 00 $5. 60 $14. 00 $89. 60 // Calculates total owed, assuming 8% tax and 15% tip public static void results(double subtotal) 4

printf answer . . . // Calculates total owed, assuming 8% tax and 15%

printf answer . . . // Calculates total owed, assuming 8% tax and 15% tip public static void results(double subtotal) { double tip = subtotal * 0. 20; double total = subtotal + tax + tip; System. out. printf("Subtotal: System. out. printf("Tax: System. out. printf("Tip: System. out. printf("Total: } } Output: Subtotal: Tax: Tip: Total: $%. 2 fn", subtotal); tax); tip); total); $70. 00 $5. 60 $14. 00 $89. 60 Another example, just the printf()s and output System. out. printf("Subtotal: $%8. 2 fn", System. out. printf("Tax: $%8. 2 fn", System. out. printf("Tip: $%8. 2 fn", System. out. printf("Total: $%8. 2 fn", Subtotal: $ 70. 00 Tax: $ 5. 60 Output: Tip: $ 14. 00 Total: $ 89. 60 subtotal); tax); tip); total); 5

Examples of printf format specifiers: Type int Specifier Example %d %5 d %-5 d

Examples of printf format specifiers: Type int Specifier Example %d %5 d %-5 d Output 123 128 double %f %8. 1 f %8. 4 f %-8. 2 f 1. 23 String %s %10 s %-10 s hello 1. 2300 1. 23 hello First value (5, 8 or 10 above) is the field width. No value means as much space as required. . value (e. g. , . 1, . 4) number of decimal places - (minus) means left justified. 6

7

7

Example: Improving the tree height calculator Original using tabs: System. out. println(year + "t"

Example: Improving the tree height calculator Original using tabs: System. out. println(year + "t" + round(beginning. Height) + "t" + growth + "t" + round(ending. Height) + "t”); Beginning Ending year Height Growth Height 1 30. 0 0. 9 30. 9 2. 93 33. 83 3. 01 36. 84 Improved using printf: System. out. printf("%4 d %8. 2 f n", year, beginning. Height, growth, ending. Height); year 1 2 3 Beginning Height 30. 00 30. 90 33. 83 Growth 0. 90 2. 93 3. 01 Ending Height 30. 90 33. 83 39. 95 8

Throwing Exceptions When the computer can compile a program but an internal error occurs

Throwing Exceptions When the computer can compile a program but an internal error occurs in execution, it throws an exception. Examples: • Divide by zero. • Attempt to access beyond the end of a string • Type mismatch on input (expects areal value, gets a character, etc. • Etc. 1 public class Cause. Exception { public static void main(String[] args) { 2 int x = 1/0; 3 System. out. println(x); 4 } 5 6 } Console: Exception in thread "main" java. lang. Arithmetic. Exception: / by zero at Cause. Exception. main(Cause. Exception. java: 3) This says the exception was thrown in line 3 of the main method within class Cause. Exception. 9

public class Reverse { public static void main(String[] args) { String original = "This

public class Reverse { public static void main(String[] args) { String original = "This is me. "; String reverse = reverse. It(original); System. out. println("The original string was "" + original + ""n. The reversed string is "" + reverse + """); } public static String reverse. It(String param) { String temp = ""; ; i >= i--) for (int i = param. length(); i >=0; 0; temp += param. char. At(i); return temp; } } Exception in thread "main" java. lang. String. Index. Out. Of. Bounds. Exception: String index out of range: 11 at java. lang. String. char. At(Unknown Source) at Reverse. reverse. It(Reverse. java: 12) at Reverse. main(Reverse. java: 5) 10

public class Test. Index. Of { public static void main(String[] args) { String first

public class Test. Index. Of { public static void main(String[] args) { String first = "This is the first string to test"; System. out. println("Found letter 'f' at index " + index. Of('f', first)); System. out. println("Found letter 'q' at index " + index. Of('q', first)); } public static int index. Of(char c, String s){ for (int i = 0; i <= s. length(); i++ ) if (s. char. At(i) == c ) return i; return -1; } } Exception in thread "main" java. lang. String. Index. Out. Of. Bounds. Exception: String index out of range: 32 Why does the exception at java. lang. String. char. At(Unknown Source) happen? at Test. Index. Of. index. Of(Test. Index. Of. java: 14) at Test. Index. Of. main(Test. Index. Of. java: 9) Why is the exception displayed Found letter 'f' at index 12 before the index of ‘f’? 11