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

后端 未结 6 1541
耶瑟儿~
耶瑟儿~ 2020-11-28 14:27

Here is my code:

int hoursFormat = 1, minsFormat = 2, secsFormat = 3;
String timeFormat = String.format(\"%02d:%02d:%02d\",hoursFormat, minsFormat, secsForma         


        
相关标签:
6条回答
  • 2020-11-28 14:51

    Are you using Java version 1.4 or below? Autoboxing wasn't introduced until Java 5. Try manually boxing ints to Integers.

    0 讨论(0)
  • 2020-11-28 14:56

    You have jdk 1.4 or below version. The method String.format() is introduce in Jdk 1.5.

    Take a look at coderanch post.

    0 讨论(0)
  • 2020-11-28 14:59

    I had a similar problem with printf. I am using JDK 1.6.0_27. Setting the compliance level to 1.6 solved my issue. This can be set in the following way.

    Project > Properties > Java Compiler

    You can also refer to the following thread:

    Why am I getting a compilation errors with a simple printf?

    0 讨论(0)
  • 2020-11-28 15:03

    I'm gonna vote for a clean build. There's no reason it should be failing like this, especially when you try manually boxing to Integer. Are you using Eclipse? Sometimes it gets confused, and you just have to rebuild everything.

    0 讨论(0)
  • 2020-11-28 15:13

    Are you using eclipse?

    If so sometimes, issues like this appear, when everything seems to be correct. Here is how I just solved it:

    • Right click on project and go to properties->Java Compiler
    • You would be seeing a recent Compiler compliance level (1.7 in my case) set in the drop down ("compiler compliance level"). Also same version is seen set below in "Generated .class files compatibility" and "Source compatibility".

    Now:

    • Select the Checkbox: "Use default compliance settings"
    • Notice that a lower version (in my case 1.1) got set for: "Generated .class files compatibility" and "Source compatibility". This is the issue, although eclipse is showing that it is compiling using a higher compiler it is not.
    • In the drop down "compiler compliance level" choose some other level and then select the one you want. The changes would be reflected below in "Generated .class files compatibility" and "Source compatibility".

    This should have resolved the issue.

    0 讨论(0)
  • 2020-11-28 15:14

    The signature of format is:

    public static String format(String format, Object ... args) {
        return new Formatter().format(format, args).toString();
        }
    

    and it seems no any mistakes in your code, for my suggestion try to run this with console application without using any IDE (For test purpose).

    0 讨论(0)
提交回复
热议问题