String.format() throws FormatFlagsConversionMismatchException

爷,独闯天下 提交于 2019-12-01 05:22:22

You asked for a workaround; just use StringBuilder:

public static String padLeft(String s, int n) {
    if (n <= 0)
        return s;
    int noOfSpaces = n * 2;
    StringBuilder output = new StringBuilder(s.length() + noOfSpaces);
    while (noOfSpaces > 0) {
        output.append(" ");
        noOfSpaces--;
    }
    output.append(s);
    return output.toString();
}

Since you are using # flag in format string, you should pass Formattable as an argument (doc).

Any work arounds?

Don't use # in format string?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!