1

I am trying understand why one of my printf statements fail. #1 below is fine, but #2 fails with the exception at the end. What's causing this?

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)

Any ideas what's causing this?

1

4 Answers 4

3

Here you go

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);

I have replaced " with ' ( looks simplified ) will work like charm as in html " or ' do great.
For % , it should be %% as % is escaped with %

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is with the percent sign. Since % is used as a format string. You can insert % as a literal preceding it with another percent... %%

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

Comments

1

The exception says that %" is not a valid format specifier. You should escape your % sign in line 2:

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

Comments

0
%\"

Is not a valid printf field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.