String.format

23 views
Skip to first unread message

mifrai

unread,
Apr 7, 2008, 3:27:41 AM4/7/08
to Clojure
I can't seem to get (. String (format ..)) to work at all.

It works in java:

class test {
public static void main(String[] args) {
System.out.println(String.format("%2s, %s", 1, 2));
}
}

But in clojure:

user> (let [a 1, b 2] (println (. String (format "%2s, %s" a b))))
java.lang.ClassCastException: java.lang.String cannot be cast to
java.util.Locale
...

user> (. String (format "%2s, %s" 1 2))
java.lang.ClassCastException
...

user> (. String (format (java.util.Locale.US) "%2s, %s" 1 2))
clojure.lang.Compiler$CompilerException: NO_SOURCE_FILE:1: No
matching method: format
...


Any ideas?

Arto Bendiken

unread,
Apr 7, 2008, 3:45:16 AM4/7/08
to Clojure
mifrai wrote:
> I can't seem to get (. String (format ..)) to work at all.

You have to wrap the arguments with '(to-array ...)'. See this thread
for more information:

http://groups.google.com/group/clojure/browse_thread/thread/d2f91070209720e1

You can grab functioning versions of printf/sprintf from:

http://svn.bendiken.net/clojure/util.clj

Arto

Reply all
Reply to author
Forward
0 new messages