Formatting text with printf Up to now it

  • Slides: 11
Download presentation
Formatting text with printf Up to now it has been difficult to control how

Formatting text with printf Up to now it has been difficult to control how output, especially double values appeared on the output. printf stands for print formatted. Needed format specifiers will be presented in this class. They are also available on page 260 of the text. They are easy to forget. 1

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 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 2

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 330

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 4

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

printf . . . // 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: $%8. 2 fn", subtotal); System. out. printf("Tax: $%8. 2 fn", tax); System. out. printf("Tip: $%8. 2 fn", tip); System. out. printf("Total: $%8. 2 fn", total); } } Output: Subtotal: Tax: Tip: Total: $ $ 70. 00 5. 60 14. 00 89. 60 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

TABLE 4. 6 Common Format Specifiers (page 260) 7

TABLE 4. 6 Common Format Specifiers (page 260) 7

You can build your own string portion of the printf using concatenation. public class

You can build your own string portion of the printf using concatenation. public class Printf. Var { public static void main(String[] args) { for ( int i = 1; i <= 10; i++) System. out. printf("The value of PI is %20. " + i + "f n", Math. PI); } } The The The value value value of of of PI PI PI is is is 3. 142 3. 1416 3. 141593 3. 1415927 3. 141592654 3. 1415926536 8

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

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