Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.format() takes an array as a single argument

Why this does work OK?:

String f = "Mi name is %s %s.";
System.out.println(String.format(f, "John", "Connor"));

And this doesnt?:

String f = "Mi name is %s %s.";
System.out.println(String.format(f, (Object)new String[]{"John","Connor"}));

If the method String.format takes a vararg Object?

It compiles OK but when I execute this the String.format() takes the vararg Object as a single an unique argument (the toString() value of the array itself), so it throws a MissingFormatArgumentException because it cannot match with the second string specifier (%s).

How can I make it work? Thanks in advance, any help will be greatly appreciated.

like image 527
mevqz Avatar asked Jul 18 '12 05:07

mevqz


People also ask

What is string format () and how can we use it?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Parameter: The locale value to be applied on the format() method.

How do I format an array of strings?

str = compose( formatSpec , A ) formats data values from the input array, A , using formatting operators specified by formatSpec and returns the resulting text in str . The compose function formats values from A in column order. If formatSpec is a string array, then so is the output array str .

Can we use string as an array?

We can have an array with strings as its elements. Thus, we can define a String Array as an array holding a fixed number of strings or string values. String array is one structure that is most commonly used in Java.

How do you take an array as an argument in Java?

To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type. Given below is the method prototype: void method_name (int [] array);


2 Answers

Use this : (I would recommend this way)

String f = "Mi name is %s %s.";
System.out.println(String.format(f, (Object[])new String[]{"John","Connor"}));

OR

String f = "Mi name is %s %s.";
System.out.println(String.format(f, new String[]{"John","Connor"}));

But if you use this way, you will get following warning :

The argument of type String[] should explicitly be cast to Object[] for the invocation of the varargs method format(String, Object...) from type String. It could alternatively be cast to Object for a varargs invocation.

like image 105
Nandkumar Tekale Avatar answered Sep 20 '22 08:09

Nandkumar Tekale


The problem is that after the cast to Object, the compiler doesn't know that you're passing an array. Try casting the second argument to (Object[]) instead of (Object).

System.out.println(String.format(f, (Object[])new String[]{"John","Connor"}));

Or just don't use a cast at all:

System.out.println(String.format(f, new String[]{"John","Connor"}));

(See this answer for a little more info.)

like image 24
Ted Hopp Avatar answered Sep 22 '22 08:09

Ted Hopp