0

I am new to Java and have been working through a book a buddy of mine is letting me borrow. However, I believe the book may be a bit out of date.

I am trying to print a price, and to do so the book uses the example (packPrice & packVolume are both double variables entered using in.nextDouble()):

double pricePerOunce = packPrice / packVolume;

System.out.printf("Price per ounce: %8.2f", pricePerOunce);
System.out.println();

However, when I try the exact same code in Java SE 8u5 (Windows 64 bit), I get the 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, double)

Does anyone have the remedy to my problem? Let me know if any more information is needed.

Thanks in advance!

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • Are you using an IDE? If so, what language compatibility level is it set to? If it's set to 1.4 or below, you'll get this error message. `printf` uses vararg methods, a feature that wasn't introduced until Java 1.5 (aka Java 5). – yshavit May 14 '14 at 19:50

2 Answers2

0

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

"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."

Community
  • 1
  • 1
AdamK
  • 177
  • 10
0

Another possible way of outputting a double with decimal precision is done like this,

double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));

This example and similar question can be found here: set double format with 2 decimal places

Community
  • 1
  • 1
Rob Steiner
  • 125
  • 1
  • 10