I was trying to pass a string object to System.out.printf(…) in Java, but I kept getting this error "java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags = 0" which actually doesn’t make a lot of sense to me.
String format = "%" + (3 * n) + "s"; // n is an int defined somewhere above, could be 0
System.out.printf(format, "My string");
Does anyone know if this is allowed?
Edit for more details:
int n = 0;
String fm = "%" + (3 * level) + "s";
String realFm = "%0s";
System.out.println("fm = " + fm);
System.out.println("realfm = " + realFm);
System.out.println("equals? " + (fm.equals(realFm)));
System.out.printf(fm, " ");
Here’s the output:
fm = %0s
realfm = %0s
equals? true
java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags = 0
Thanks
Yes it’s allowed, but it won’t work if n is 0. Is there a chance of this happening (looks like it from the error message)?
what if you add a line to your code to debug it:
Does it ever print
n is: 0?e.g.,