File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments