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
Jan Thomä - 27 Aug 2007 07:31 GMT
> 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ä

Signature
_________________________________________________________________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de