3
votes

I want to do something in ColdFusion that is similar to sprintf in C or Perl. I found this answer, which seems to be what I'm looking for. However, I can't get it to work.

Here is what I'm trying:

<cftry>
    <cfset firstName="John">
    <cfset output=createObject("java","java.lang.String").format("Hello, %s!", firstName)>
    <cfcatch type="any">
        <cfdump var="#cfcatch#" expand="false">
    </cfcatch>
<cftry>

And here is what I get:

cfcatch.Message: The format method was not found.

cfcatch.Detail: Either there are no methods with the specified method name and argument types or the format method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the provided arguments. If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity.

This is an overloaded method, so I did as suggested and used JavaCast on the arguments:

<cfset output=createObject("java","java.lang.String").format(JavaCast('string', "Hello, %s!"), firstName)>
<cfset output=createObject("java","java.lang.String").format("Hello, %s!", JavaCast('string', firstName))>
<cfset output=createObject("java","java.lang.String").format(JavaCast('string', "Hello, %s!"), JavaCast('string', firstName))>

and got the same error every time.

I tried another static method on the String class, valueOf, and it worked fine.

Edit: I've already seen a comment, and I'm not sure how to respond to those, but maybe I should explain here. What I've shown above is an extremely simplified example of what I am trying to do. The goal is to use a format string to provide lots of formatting in one place, and then simply pass in a list of variables, instead of formatting a bunch of variables and outputting them, or formatting them as I'm outputting them. With the format method, I plan to build a set of format strings that match the output I need, then I will just cfloop or cfoutput over a query, run this one method inside, and get the output I want. No DateFormat, NumberFormat, Left, Right, etc. If I can't get this working, that is plan B though.

I'm running ColdFusion 9.01, Windows 7, Java 1.6.0_22.

Any and all help is greatly appreciated.

1
I smell an XY Problem. Why exactly can you not simply go: <cfoutput>#firstname#</cfoutput> ?Shawn Holmes
FYI: It probably can't match the format method you're looking for because its signature is format(String, Object[]) and your second String argument can't be automatically converted to an Object array.Torious
The linked example is wrong. The second argument should be an array output= javacast("string", "").format("Hello %s", ["John"]). (Edit) But can you elaborate on your ultimate goal? Because this does seem like an over-complication ..Leigh
Thanks Torious and Leigh! That took care of it. I'm brand new at asking questions here, any suggestions at how to mark this answered when it was answered in the comments?Barry
Well typically my motto is use java only when something cannot be accomplished easily in CF. I know format is appealing because it is a single function and does have a little more pattern flexibility IIRC. However, since it is direct java access it is more strict about the input. Not to mention you lose some of the automatic casting offered by the CF functions. So you may want to run some tests to be sure it will work properly in all of the expected conditions. As for answering, @Torious should promote his "comment" to an "answer". Then you can select it (welcome to stackoverflow btw :)Leigh

1 Answers

1
votes

Answer based on my comment above:

It probably can't match the format method you're looking for because its signature is format(String, Object[]) and your second String argument can't be automatically converted to an Object array.

You could change the argument to encapsulate the name in an array as follows: format("Hello, %s!", [firstName]).

Thanks to Leigh for being so courteous, I upvoted your comments :)