views:

201

answers:

5

I am trying to use a .format method of a string. But if I place %1, %2 etc in the string, java.util.UnknownFormatConversionException is thrown pointing to a confusing Java source code piece:


private void checkText(String s) {
  int idx;
  // If there are any '%' in the given string, we got a bad format
  // specifier.
  if ((idx = s.indexOf('%')) != -1) {
  char c = (idx > s.length() - 2 ? '%' : s.charAt(idx + 1));
  throw new UnknownFormatConversionException(String.valueOf(c));
  }
}

From this I understand that % char is forbidden. If so, then what to use for argument placeholders?

I use Scala 2.8.

+2  A: 

You don't need to use numbers to indicate positioning. By default, the position of the argument is simply the order in which it appears in the string.

Here's an example of the proper way to use this:

String result = String.format("The format method is %s!", "great");
// result now equals  "The format method is great!".

You will always use a % followed by some other characters to let the method know how it should display the string. %s is probably the most common, and it just means that the argument should be treated as a string.

I won't list every option, but I'll give a few examples just to give you an idea:

// we can specify the # of decimals we want to show for a floating point:
String result = String.format("10 / 3 = %.2f", 10.0 / 3.0);
// result now equals  "10 / 3 = 3.33"

// we can add commas to long numbers:
result = String.format("Today we processed %,d transactions.", 100000);
// result now equals  "Today we processed 1,000,000 transactions."

String.format just uses a java.util.Formatter, so for a full description of the options you can see the Formatter javadocs.

And, as BalusC mentions, you will see in the documentation that is possible to change the default argument ordering if you need to. However, probably the only time you'd need / want to do this is if you are using the same argument more than once.

TM
+5  A: 

You need to specify the value type. You probably want to use s (String).

String string = "first: %s, second: %s";
String formatted = String.format(string, "one", "two");
System.out.println(formatted); // first: one, second: two

If your sole intent is to mark the positions so that you can control the ordering where the parameters are to be applied (default is left to right), then use %n$s instead where n is the index.

String string = "second: %2$s, first: %1$s";
String formatted = String.format(string, "one", "two");
System.out.println(formatted); // second: two, first: one

See also:

BalusC
+1  A: 

Instead of looking at the source code, you should read the javadoc String.format() and Formatter syntax

You specify the format of the value after the %, for instance for decimal integer is d, for String is s :

String aString = "world";
int aInt = 20;
String.format("Hello, %s on line %d",  aString, aInt );

output:

Hello, world on line 20

To do what you tried ( use an argument index ) you use: n$

String.format("Line:%2$d. Value:%1$s. Result: Hello %1$s at line %2$d", aString, aInt );

output

Line:20. Value:world. Result: Hello world at line 20
OscarRyz
+4  A: 

While all the previous responses are correct, they're all in Java. Here's a Scala example:

val string = "Hello %s, isn't %s cool?"
val formatted = string.format("Ivan", "Scala")

I also have a blog post about making format like Python's % operator that might be useful.

pr1001
+1  A: 

Also note that Scala extends String with a number of methods (via implicit conversion to a WrappedString brought in by Predef) so you could also do the following:

val formattedString = "Hello %s, isn't %s cool?".format("Ivan", "Scala")
dpp