The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (...)
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.
Solution 1:
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
.
Solution 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.
Solution 3:
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.
Solution 4:
Use: System.out.printf(arg0, arg1, arg2)
instead of System.out.printf(arg0, arg1)
.