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

Discussion in 'Java' started by ankur, Aug 26, 2007.

  1. ankur

    ankur Guest

    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
    ankur, Aug 26, 2007
    #1
    1. Advertising

  2. ankur wrote:
    > 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ä


    --
    _________________________________________________________________________
    insOMnia - We never sleep...
    http://www.insOMnia-hq.de
    Jan =?UTF-8?B?VGhvbcOk?=, Aug 27, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith
    Replies:
    1
    Views:
    475
    Keith
    Feb 8, 2004
  2. Tristan
    Replies:
    1
    Views:
    409
    Hugo Pragt
    Jan 26, 2004
  3. -
    Replies:
    2
    Views:
    358
    Roedy Green
    Jul 24, 2005
  4. Ninan
    Replies:
    4
    Views:
    370
    Howard
    Jun 29, 2006
  5. TheSouthLondonSlasher

    JUnit 4 Test Suite - addTest not applicable?

    TheSouthLondonSlasher, Sep 24, 2008, in forum: Java
    Replies:
    4
    Views:
    17,493
    Arne Vajhøj
    Oct 8, 2008
Loading...

Share This Page