13 Replies Latest reply: Nov 30, 2006 10:00 PM by 796365 RSS

    String format error

    807607
      Really sorry for the basic question,
      i have a problem when using: System.out.printf("%d%s",5,"sdf");

      message is : "The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int, String)"

      same problem also happens when i turn to String.Format.

      Could someone help to handle this issue?
      Thank you very much in advance.
        • 1. Re: String format error
          807607
          Complement, I use Eclipse3.1.2 and JRE1.5.0_09.
          Thank you!
          • 2. Re: String format error
            807607
            There is no problem in the IDE and JRE u r using...Firstly I will like to make you to say something on "System", "out" and"print"..now "System":-its a public and final class so that its accessible everywhere but u cant extend it...now "out" is a "static" method defined inside this class and it has the return type of "PrintStream".Now as we know that whenever you want to display some data through stream (which u r tring to do) you can pass just one argument to the function which is sending the data to stream..in your code you can try anything such as:- System.out.println(3,4)(IT WILL GIVE YOU THE ERROR) or System.out.println("3","4")(IT WILL AGAIN GIVE THE ERROR)...In short you can pass just one argument to the out function at a time..it doesnt recognise the "," seperator....by putting more than one argument in out function u r violating the "contract" of System class....

            I hope your query is resolved
            • 3. Re: String format error
              807607
              Works fine for me.
              • 4. Re: String format error
                796365
                Previous posters please note that the method is
                printf
                not
                print
                As the error states,there is no printf method that takes arguments (String, int, String).

                What are you trying to do by using these parameters?
                • 5. Re: String format error
                  807607
                  Thank you for your explain.
                  But i just want to use a C style string format in JAVA, is it impossible?
                  Anyway, I saw the syntax in one Dr.Deitel's book.
                  • 6. Re: String format error
                    807607
                    I am really sorry but I didnt see printf...I thought it as print...
                    Yes it should work fine coz sun does provide the facility to use printf for the formatted output...
                    • 7. Re: String format error
                      807607
                      As mentioned before it works for me. So kk can you post some more code so we can see what you are trying to do. To retain formatting, highlight the code and then click the code button above message box.
                      • 8. Re: String format error
                        807607
                        Yes Java does provide the facility to exploit some features of C..But that too in very limited cases...Java is more restricted language than C..for example if you try to put wrong specifier after % signthen you wud get the error(FormatException something like that) but in acse of C its ignored....You can make your own specifiers by makin changes in Locale object...
                        • 9. Re: String format error
                          807607
                          Thank you all.
                          I just begin to learn, and try this

                          public class Array_test {
                          public static void main(String[] args) {
                               int a=5;
                               System.out.printf("%d",a);
                          }
                          }

                          really sad it doesn't work.
                          May it some problem happen in the auto-typeconverter in my place? For it need to "convert int to object[]"
                          • 10. Re: String format error
                            807607
                            That should work. Can you copy and paste the exact error message?
                            • 11. Re: String format error
                              807607
                              Exception in thread "main" java.lang.Error: Unresolved compilation problem:
                                   The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)

                                   at Array_test.main(Array_test.java:5)
                              • 12. Re: String format error
                                807607
                                Ok,
                                it works in my place after i changed the default compliance level 1.4 to 5.0 in Windows->preferences->java->complier, though i did not know why deeply.
                                • 13. Re: String format error
                                  807607
                                  Varags (which is what printf exploits to allow you to enter any number of arguments) only became available in Java 1.5. Therefore if you try to compile as a Java 1.4 program it rightly complains.