4

Scala code I have

System.out.println(String.format("%s %6d", "someName", someLong))

and the error:

 overloaded method value format with alternatives:
[error]   (java.util.Locale,java.lang.String,<repeated...>[java.lang.Object])java.lang.String <and>
[error]   (java.lang.String,<repeated...>[java.lang.Object])java.lang.String
[error]  cannot be applied to (java.lang.String, Long, String)
[error]       System.out.println(String.format("%s %6d",  "someName", someLong))

How do I solve this?

2 Answers 2

6

To illustrate Randall's answer.

If you're over fifty, use Randall's answer, but if you're under forty or can still wear fitted shirts, use the f-interpolator! Life is short.

scala> String.format("%s %6d", "someName", someLong: java.lang.Long)
res8: String = someName      5

scala> val someName = "Bob"
someName: String = Bob

scala> f"${someName} ${someLong}%6d"
res10: String = Bob      5
0
2

The reason what you're trying doesn't work is that the Java code cannot handle Scala's AnyVal types.

Instead, use Scala's counterpart:

println("%s %6d".format("someName" someLong))

Or, more succinctly:

printf("%s %6d%n", someName, someLong)
2
  • the first example seems incorrect, but I even can't figure out how to fix it. Jul 2, 2013 at 3:38
  • Sorry, that was very clumsy of me. It is fixed, now. Jul 2, 2013 at 3:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.