Skip to content

Commit 65c0a05

Browse files
author
Seth Underhill
committed
Exercise 3.1
1 parent a15b84d commit 65c0a05

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

ch03/WhatHappensPrintf.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
public class WhatHappensPrintf {
3+
/**
4+
Exercise 3.1 When you use printf, the Java compiler does not check your
5+
format string. See what happens if you try to display a value with type int
6+
using %f. And what happens if you display a double using %d? What if you
7+
use two format specifiers, but then provide only one value?
8+
**/
9+
public static void main (String[] arguments) {
10+
final int x = 10;
11+
final double y = 3.25;
12+
13+
//print int with %f
14+
System.out.printf("Print int with floating point format mask %f", x);
15+
// throws java.util.IllegalFormatConversionException
16+
17+
//print double with %d
18+
System.out.printf("Print double with integer format mask %d", y);
19+
// throws java.util.IllegalFormatConversionException
20+
21+
//print with format specifiers but only one argument
22+
System.out.printf("Print with two format specifiers %d and %f, but with only one argument supplied.", x);
23+
// throws java.util.MissingFormatArgumentException
24+
}
25+
}

0 commit comments

Comments
 (0)