【问题标题】:The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (...)PrintStream 类型中的方法 printf(String, Object[]) 不适用于参数 (...)
【发布时间】:2011-12-08 03:13:11
【问题描述】:

为什么我通过简单的 printf 调用得到以下编译错误?我的代码:

import java.util.Scanner;

public class TestCodeBankAccInputs
{
    public static void main(String[] args)
    {
        String displayName = "Bank of America Checking";
        int balance = 100;
        System.out.printf("%s has %7.2f", displayName, balance);
    }
}

编译时出现以下错误:

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, String, double)
  at TestCodeBankAccInputs.main(TestCodeBankAccInputs.java:9)

这是什么原因造成的,我该如何解决?

版本信息:

Help->About in Eclipse 提供以下信息:

面向 Web 开发人员的 Eclipse Java EE IDE。

版本:Indigo 发布 内部版本号:20110615-0604

我安装的JDK是JDK1.6.0_27

我见过this similar issue regarding String.format。一些用户认为这可能是构建问题,但看起来我已经更新了版本。

【问题讨论】:

    标签: java


    【解决方案1】:

    检查 Compiler compliance level 是否为您的项目设置为至少 1.5:

    项目 > 属性 > Java 编译器

    如果未设置Enable project specific settings,请使用该页面上的Configue Workspace Settings... 链接检查全局Compiler compliance level

    【讨论】:

    • @Carlos 我已启用项目特定设置仍然存在问题,我使用 Kepler 任何见解
    • 有人在 IDEA 15 中看到这个选项吗?
    • @phillipsK 也许这有助于stackoverflow.com/questions/12745510/…
    【解决方案2】:

    这似乎很奇怪,再次出现(与您链接的其他帖子相同的问题)。我想知道最新版本的 Eclipse 中是否存在错误?另一个帖子上的提问者再也没有回来提供更多信息,所以我怀疑它可能已经消失了。您的代码运行良好。如果我提供适当的 BankAccount 类,它会在 IntelliJ 10.5.2 和命令行中按预期编译和运行 javacjava,版本 1.6.0_26:

    import java.util.Scanner;
    
    public class TestCodeBankAccInputs {
        public static void main(String[] args) {
            Scanner inStream = new Scanner(System.in);
            BankAccount myAccount = new BankAccount(100, "Bank of America Checking");
            System.out.print("Enter a amount: ");
            double newDeposit = inStream.nextDouble();
            myAccount.deposit(newDeposit);
    
            System.out.printf("%s has %9.2f", myAccount.displayName(), myAccount.getBalance());
            //System.out.printf("%3s", "abc");
        }
    
        static class BankAccount {
    
            private double balance;
            private String name;
    
            public BankAccount(double balance, String name) {
                this.balance = balance;
                this.name = name;
            }
    
            public String displayName() {
                return name;
            }
    
            public double getBalance() {
                return balance;
            }
    
            public void deposit(double newDeposit) {
                this.balance += newDeposit;
            }
        }
    }
    

    我仍然(就像我在另一篇文章中所做的那样)推荐一个干净的构建,但是您是否检查过您在 Eclipse 中的编译器合规性级别?您可以使用 1.6 JDK 进行编译,但仍然在 IDE 中设置了较低的合规级别,这可能会发生一些有趣的事情。

    【讨论】:

    • 确实是编译器合规级别!!!我将它设置为 1.6 并且代码编译没有错误并且运行良好。感谢所有人,尤其是 Carlos、Ryan、Bohemian、Javed 提供的大力支持和帮助!!!
    【解决方案3】:

    这样的临时修复可能会起作用。

    不要使用printf,而是使用这个:

    System.out.printf("%s has %7.2f", new Object[]{
        myAccount.displayName(), myAccount.getBalance()
    } );
    

    这可能会解决您的问题。

    【讨论】:

    • 是的,这可能会使其工作,但另一方面,当前代码没有理由不工作,并且完全添加了可变参数,因此您不必编写如此丑陋的代码.
    【解决方案4】:

    使用:System.out.printf(arg0, arg1, arg2) 而不是 System.out.printf(arg0, arg1)

    【讨论】:

      猜你喜欢
      • 2012-06-24
      • 2016-05-28
      • 2017-08-15
      • 2011-12-03
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      相关资源
      最近更新 更多