Why can't I pass a variable as format string to System.out.printf (…) in java?


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



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:

// line added below for debugging. To remove later: System.out.println("n is: " + n); String format = "%" + (3 * n) + "s"; // n is an int defined somewhere above System.out.printf(format, "My string");

Does it ever print n is: 0?


public static void main(String[] args) { // try the for loop with n = 0 vs. n = 1 for (int n = 1; n <= 10; n++) { // line added below for debugging. To remove later: System.out.println("\nn is: " + n); String format = "%" + (3 * n) + "s"; System.out.printf(format, "My string"); } }


  • java.io.EOFException when sending serialized data to a servlet
  • Different results when setting/getting System properties with GStrings in Groovy
  • Why would a Scala Worksheet using Scala-IDE give this as an error?
  • Java, will (low + high) >>> 1 overflow?
  • Is the Go HTTP handler goroutine expected to exit immediately in this case?
  • Read a file in “chunks” using PHP
  • Better file search algorithm than creating a list of files
  • Leaflet z-index
  • Smack 4.1.0 android Roster not displaying
  • auth.provider is not set to 'password' when user signs-in with email and password
  • How can I extend PHP DOMElement?
  • If statement skipping right to else after being called once?
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • Filter strings with regex before casting to numeric
  • Reading a file into a multidimensional array
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • App restarts from wrong activity
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • Jetty Server not starting: Unable to establish loopback connection
  • Fetching methods from BroadcastReceiver to update UI
  • Get object from AWS S3 as a stream
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • Symfony2: How to get request parameter
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • GridView Sorting works once only
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • KeystoneJS: Relationships in Admin UI not updating
  • WPF Applying a trigger on binding failure
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Sorting a 2D array using the second column C++
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF