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

今天,我弟遇到一个有意思的错误~

程序:

package com.mq.ceshi1;

public class StringFormat {
  public static void main(String[] args) {
    int num = 10;
    int num2 = 5;
    System.out.println(String.format("%d / %d = %d", num,num2,num / num2));
  }
}

报了The method format(String, Object[]) in the type String is not applicable for the arguments (String, int,int,int)错误。

首先分析一下,jdk文档:

可见,这个是在1.5版本后添加的。

后来,根据网上的修改,按以下运行也是正常的。

package com.mq.ceshi1;

public class StringFormat {
  public static void main(String[] args) {
    // int num = 10;
    // int num2 = 5;
    Integer [] nums = {10,5,2};
    // System.out.println(String.format("%d / %d = %d", num,num2,num / num2));
    System.out.println(String.format("%d / %d = %d", nums));
  }
}

查看了出问题机器使用的是:jdk版本也是1.7.8  myecliplse8.6

由于版本大于1.5,但是我还是怀疑是版本引起的。于是,在有问题的机器上,由使用了1.5版本之后才有的自动拆装箱机制。

Integer num = 5;  //发现报错

进一步验证了我关于版本可能带来的错误。

于是,将myecliplse版本升级到2014版,发现问题消失。

总结:怀疑是myeclipse8.6版本与jdk1.7.8存在不兼容的问题。(暂无直接证据,如果哪位有方法验证的话,请不吝赐教!!)

原文地址:https://www.cnblogs.com/muxi0407/p/6680475.html