18

Why do I get the following compilation error with a simple call to printf? My code:

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);
    }
}

On compilation I get the following error:

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)

What's causing this and how can I fix it?

Version information:

Help->About in Eclipse gives following information:

Eclipse Java EE IDE for Web Developers.

Version: Indigo Release Build id: 20110615-0604

The JDK I installed is JDK1.6.0_27

I have seen this similar issue regarding String.format. Some users have suggested it might be a build issue, but looks like I have updated versions.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
yankeemike
  • 451
  • 1
  • 3
  • 8

4 Answers4

37

Check that the Compiler compliance level is set to at least 1.5 for your project:

Project > Properties > Java Compiler

if Enable project specific settings is not set, use the Configue Workspace Settings... link on that page to check the global Compiler compliance level.

enter image description here

user85421
  • 28,957
  • 10
  • 64
  • 87
2

It seems peculiar that this has popped up again (same issue as the other post you linked). I wonder if there's a bug in a recent version of Eclipse? The asker on that other post never came back with any more info, so I suspect that it might have just gone away. Your code works perfectly fine. If I supply an appropriate BankAccount class, it compiles and runs as expected both in IntelliJ 10.5.2 and from the command line with javac and java, version 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;
        }
    }
}

I still (as I did for the other post) recommend a clean build, but have you checked your compiler compliance level in Eclipse? You can be compiling with a 1.6 JDK but still have a lower compliance level set in an IDE, which can make funny things happen.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 1
    It was indeed the compiler compliance level!!! I set it to 1.6 and the code compiled without errors and ran fine. Thanks all and especially Carlos, Ryan, Bohemian, Javed for the wonderful support and help provided!!! – yankeemike Oct 15 '11 at 01:49
1

Use: System.out.printf(arg0, arg1, arg2) instead of System.out.printf(arg0, arg1).

Undo
  • 25,519
  • 37
  • 106
  • 129
1

A temporary fix like this might work.

Instead of using printf, use this:

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

This might fix your problem.

Pang
  • 9,564
  • 146
  • 81
  • 122
Osama Javed
  • 1,432
  • 1
  • 16
  • 21
  • Yes, that would likely make it work, but on the other hand, there's no reason the current code shouldn't work, and varargs were added exactly so you don't have to write such ugly code. – Ryan Stewart Oct 14 '11 at 23:52