14

I want to run a bit of Java in Scala console. Here's what I get:

scala> String.format("hello %d",3);
<console>:8: error: overloaded method value format with alternatives:
  (java.util.Locale,java.lang.String,<repeated...>[java.lang.Object])java.lang.String <and>
  (java.lang.String,<repeated...>[java.lang.Object])java.lang.String
 cannot be applied to (java.lang.String, Int)
              String.format("hello %d",3);

Why Scala can't recognize which method to call, if argument set is different, and the ones I provide are quite unambigous?

What is strange, the same message appears also when I try to call function with arguments which don't match to any of both argument sets, e.g. String.format()

I was using scala 2.9.1

4 Answers 4

14

Your arguments don't match the function prototype. You're calling the function with second argument scala.Int which is not a java.lang.Object.

Convert it to java.lang.Integer and it will work.

See also boxing and unboxing in Scala.

1
7

I recommend to use the new String interpolators available in Scala 2.10.X. They are easier to use.

  val x = 3
  String.format("hello %d", x: Integer)

would be simply:

  val x =3 
  s"hello $x"
3

Try

String.format("hello %d",3.asInstanceOf[java.lang.Object]);
0
0

Also, you can let Scala do the boxing for you:

String.format("hello %d", 3: Integer)

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.