Mike Flynn
Mike Flynn

Reputation: 24315

UnknownFormatConversionException with String.format in Java

Why am I getting the error java.util.UnknownConversionException: Conversion = 'D' for the method below. I create an intial string, then return that from a method to do a String.format. It happens at the String.format.

String url = StringUtils.join(new String[] { BASE_URL,
                "/location/inst/", slug,
                "/coursesections/number%s.json?apiKey=", apiKey,
                "&token=", GetAuthTokenEncoded(authToken) })


String url2 = String.format(url, ("/" + callNumber));

Output of StringUtils

http://test.com/location/inst/testy/coursesections/number%s;.json?apiKey=dfsdfsdfsadfsdf&token=2.0%7Cidm%7Cidm%7Ctest%3Dbesrlin_50d4s9dd25dsee56f3a1e95fb4f%26berlin%3D50d49dd2e2f28cf450b0caa9f449%26campus%3Dberlin%3A50d49dd25ee56f3a1e95fb4f%7C2013-03-12T16%3A25%3A23%2B00%3A00%7C2013-03-12T19%3A25%3A23%2B00%3A00%7C09161a6011f2dc2e3443bc40b2d3f4b3d4f

Upvotes: 1

Views: 1516

Answers (2)

DaveJohnston
DaveJohnston

Reputation: 10151

GetAuthTokenEncoded(authToken) contains a whole load of % causing errors. Why not just append the callNumber in the same way you are joining the rest of the String?

String url = StringUtils.join(new String[] { BASE_URL,
            "/location/inst/", part,
            "/coursesections/number/", number, ".json?apiKey=", apiKey,
            "&token=", GetAuthTokenEncoded(authToken) })

Upvotes: 2

rgettman
rgettman

Reputation: 178253

There is "%3D" in the url string to be formatted due to URL escaping. You will need to supply the result of GetAuthTokenEncoded as a parameter to String.format so it doesn't interpret "%3D" as a placeholder.

String url = StringUtils.join(new String[] { BASE_URL,
            "/location/inst/", part,
            "/coursesections/number%s.json?apiKey=", apiKey,
            "&token=%s");
String url2 = String.format(url, ("/" + callNumber), GetAuthTokenEncoded(authToken));

Upvotes: 1

Related Questions