Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
ankur - 26 Aug 2007 22:59 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.