Varargs and String.format

M

Mark Space

I'm having trouble passing a single argument to String.format:

String damnit = String.format( locale, format,
new Object[]{input} );

This line chooses the String.format( String, Object... ) form of the
function, rather than the String.format( String, String, Object... )
form. Any ideas how to force to the latter?

The code below throws NPE because of the error, but even if I put
something in "locale" it won't work right, because it's treating locale
as the format string.

SSCCE:

package damnit;

public class damnit {

public static void main(String[] args) {
String locale = null;
String format = "%s";
String[] input = new String[]{"one", "two", "three", "four"};

for( int i = 0; i < input.length; i++ ) {
// throws NPE
String damnit = String.format( locale, format,
new Object[]{input} );
System.out.println( damnit );
}
}
}
 
J

John B. Matthews

Mark Space said:
I'm having trouble passing a single argument to String.format:

String damnit = String.format( locale, format,
new Object[]{input} );

This line chooses the String.format( String, Object... ) form of the
function, rather than the String.format( String, String, Object... )
form. Any ideas how to force to the latter?

The code below throws NPE because of the error, but even if I put
something in "locale" it won't work right, because it's treating locale
as the format string.

SSCCE:

package damnit;

public class damnit {

public static void main(String[] args) {
String locale = null;
String format = "%s";
String[] input = new String[]{"one", "two", "three", "four"};

for( int i = 0; i < input.length; i++ ) {
// throws NPE
String damnit = String.format( locale, format,
new Object[]{input} );
System.out.println( damnit );
}
}
}


I think you need type Locale to get the desired overload:

Locale locale = null;
 
M

Mark Space

Oops, figures it would be something like that. Thanks for taking the
time to illuminate my error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top