I have a problem using the String.format() method.
So I want to create a String with a line feed, for which I use %n
.
I create the string like this:
for (int i = 0; i < authorNames.size(); i++) {
tmpReturnString += authorNames.get(i) + "%n";
}
with authorNames
beeing a ArrayList. Next I want to return the formatted String:
returnResult.setAttributes(0, String.format(tmpReturnString.substring(0, tmpReturnString.length() - 1)))
returnResult
is going to be my return.
But here I get a java.util.UnknownFormatConversionException: Conversion = '%'
.
Unfortunatly I have no clue how to fix this issue. Also, non of the other questions helped me.
Thanks in advance!
I guess you want to remove the last new line, but by removing only one character you end up with your string ending with %
.
Do this instead:
String.format(tmpReturnString.substring(0, tmpReturnString.length() - 2))
(remove 2 characters instead of just 1)
Or, even better, if you use apache commons:
String.format(StringUtils.join(authorNames, "%n"));
for (int i = 0; i < authorNames.size(); i++) {
tmpReturnString += authorNames.get(i) + "%n";
}
alway return the string with two last character is "%n". So when you use
substring(0, tmpReturnString.length() - 1)
Now the last character is '%'. The '%' is not a valid format specifier.
Detail in here: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.