Reputation: 73
My string is "1" and i had to add leading zero. I'm using
if (mystring.length() == 1) {
mystring = String.format("%02d", mystring);
}
But I get:
java.util.IllegalFormatConversionException: %d can't format java.lang.String arguments
I also already tried with "%02f". Any idea why I'm getting this error ?
Upvotes: 1
Views: 755
Reputation: 317362
Because the %d specifier requires an integer type value to format. If your string is actually a number, then parse it to a number first, then pass that to the formatter:
mystring = String.format("%02d", Integer.parseInt(mystring));
Upvotes: 2
Reputation: 48258
The error is because String.format("%02d", mystring);
is expecting a decimal number... not a string, the same with %02f which means float
take a look to the doc
Upvotes: 0