Simple error : method format(String, Object[]) is not applicable for the arguments (String, String)

A

ankur

Hi all,

I am a java newbie and I am using eclipse to write this simple code:
// Fig. 3.17: Dialog1.java
// Printing multiple lines in dialog box.
import javax.swing.JOptionPane; // import class JOptionPane

public class DialogBox
{
public static void main( String args[] )
{
String j = "abc";
//System.out.print(j);
// display a dialog with the message
String.format("This is a string %s", j);
System.out.printf("This is a simple string %s", j);
//JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" );
} // end main
} // end class Dialog1

I am getting this error message for format and printf statements:

The method format(String, Object[]) in the type String is not
applicable for the arguments (String, String)
The method printf(String, Object[]) in the type PrintStream is not
applicable for the arguments (String, String)

I am using jre : jre1.6.0_02

Can anyone help !!

Thanks,
Ankur
 
J

Jan =?UTF-8?B?VGhvbcOk?=

ankur said:
String.format("This is a string %s", j);
System.out.printf("This is a simple string %s", j);
The method format(String, Object[]) in the type String is not
applicable for the arguments (String, String)
The method printf(String, Object[]) in the type PrintStream is not
applicable for the arguments (String, String)

The compiler is telling you that there is no function String.format which
takes a two arguments of type String but only one which takes a String and
an Array of Objects. So the correct way to call these is:

// wrap the string j into an object array
String.format("This is a string %s", new Object[] { j } );

Same for the other method.

btw, you should assign the result of String.format to some other variable
otherwise the result is lost...

Best regards,
Jan Thomä
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top