From 65c0a056942024f1cba3271bde1bfe17b623c0a6 Mon Sep 17 00:00:00 2001 From: Seth Underhill Date: Mon, 13 Jun 2022 07:33:47 -0500 Subject: [PATCH] Exercise 3.1 --- ch03/WhatHappensPrintf.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ch03/WhatHappensPrintf.java diff --git a/ch03/WhatHappensPrintf.java b/ch03/WhatHappensPrintf.java new file mode 100644 index 0000000..2e1e3ff --- /dev/null +++ b/ch03/WhatHappensPrintf.java @@ -0,0 +1,25 @@ + +public class WhatHappensPrintf { +/** + Exercise 3.1 When you use printf, the Java compiler does not check your + format string. See what happens if you try to display a value with type int + using %f. And what happens if you display a double using %d? What if you + use two format specifiers, but then provide only one value? +**/ + public static void main (String[] arguments) { + final int x = 10; + final double y = 3.25; + + //print int with %f + System.out.printf("Print int with floating point format mask %f", x); + // throws java.util.IllegalFormatConversionException + + //print double with %d + System.out.printf("Print double with integer format mask %d", y); + // throws java.util.IllegalFormatConversionException + + //print with format specifiers but only one argument + System.out.printf("Print with two format specifiers %d and %f, but with only one argument supplied.", x); + // throws java.util.MissingFormatArgumentException + } +}