【问题标题】:Java printf fails : UnknownFormatConversionExceptionJava printf 失败:UnknownFormatConversionException
【发布时间】:2012-08-12 06:12:37
【问题描述】:

我试图理解为什么我的一个 printf 语句失败了。下面的#1 很好,但#2 失败,最后出现异常。这是什么原因造成的?

0       String LOCATION = "http://www.mywebsite.web/";
1       System.out.printf(" <img src=\"%sIMAGE.gif\">", LOCATION);   
2       System.out.printf(" <img src=\"%sIMAGE.gif\" width=\"25%\" height=\"25%\" border=0>", LOCATION);

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '"'
    at java.util.Formatter.checkText(Formatter.java:2519)
    at java.util.Formatter.parse(Formatter.java:2501)
    at java.util.Formatter.format(Formatter.java:2430)
    at java.io.PrintStream.format(PrintStream.java:937)
    at java.io.PrintStream.printf(PrintStream.java:838)
    at MyCode.main(MyCode.java:292)

你知道是什么原因造成的吗?

【问题讨论】:

标签: java exception printf


【解决方案1】:

给你

String LOCATION = "http://www.mywebsite.web/";
System.out.printf(" <img src='%sIMAGE.gif'>", LOCATION);   
System.out.printf(" <img src='%sIMAGE.gif' width='25%%' height='25%%' border=0>", LOCATION);

我已将" 替换为'看起来很简单)将像html "' 中的魅力 一样工作 太棒了
对于 % ,它应该是 %% 因为 %% 转义

【讨论】:

    【解决方案2】:

    问题在于百分号。由于 % 用作格式字符串。您可以在其前面插入 % 作为文字,并在其前面加上另一个百分比... %%

    System.out.printf(" <img src=\"%sIMAGE.gif\" width=\"25%%\" height=\"25%%\" border=0>", LOCATION);
    

    【讨论】:

      【解决方案3】:

      异常表明%" 不是有效的格式说明符。您应该在第 2 行转义 % 登录:

      System.out.printf(" <img src=\"%sIMAGE.gif\" width=\"25%%\" height=\"25%%\" border=0>", LOCATION);
      

      【讨论】:

        【解决方案4】:
        %\"
        

        不是有效的 printf 字段。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-05
          • 2019-09-23
          • 1970-01-01
          • 1970-01-01
          • 2021-01-06
          • 2020-08-11
          • 1970-01-01
          相关资源
          最近更新 更多