4

I'm doing a intro to programming course and I'm having issues with my Eclipse that doesn't seem to want to run printf in even its simplest form.

My code is:

package Practice;

import java.io.*;

public class Printf {

    public static void main(String[] args) throws IOException
    {
        int num1 = 54;
        int num2 = 65;    
        int sum = num1 + num2;
        System.out.printf("Sum of %d and %d is %d.", num1, num2, sum);
    }
}

The error is as saying

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

I have the Java compiler compliance level set to 1.8 so it should have no issues as I have read in previous posts. Kepler version of Eclipse has had the Java 8 patch applied (so I could comply with 1.8)

No site I have found has given me any other clues as to what the issue could be?

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
Ligardian
  • 71
  • 1
  • 1
  • 5
  • An `int` is not an `Object`, so a sequence of `int` arguments can not be interpreted as a sequence of `Object`s for a varargs method. – Raedwald Jun 14 '14 at 10:55
  • 2
    @Raedwald: sure it can: the ints will be boxed to Integer. The problem is elsewhere. This code compiles and runs fine. – JB Nizet Jun 14 '14 at 11:28
  • @JBNizet The compiler should be able to do the multi-stage inference in this case? I'm impressed. – Raedwald Jun 14 '14 at 11:34
  • To add further detail in running as my Java JDK - Java SE 8u5 – Ligardian Jun 14 '14 at 11:37
  • 1
    Either it's a bug in the JDK compiler, or it's a bug in the Eclipse compiler. The JDK compiler doesn't this code fine, even in 1.6. – JB Nizet Jun 14 '14 at 11:44
  • 1
    There's nothing wrong with the code itself. It does not require Java 1.8 either. Java 1.5 and up will work. Eclipse is clearly confused. – Boann Jun 14 '14 at 12:33

3 Answers3

3

Turns out the project folders in Eclipse run on their own properties seperate from the Eclipse main settings .. in order for them to compile on version 1.6/1.7/1.8 you have to change their properties seperatly from the main settings...

Basically instead of going to windows > preferances ... to change and update the compiler version you right click on the project folder in Package Explorer > Properties > Java Compiler then check the Enable project specific settings and then Change the settings below to the compiler compliance level of 1.5 or higher in order for this code above to work.

Why it does that i have no idea makes no sence but it at least works now :)

Ligardian
  • 71
  • 1
  • 1
  • 5
1

Try using System.out.format("Sum of %d and %d is %d.", num1, num2, sum); instead of printf

jhobbie
  • 1,016
  • 9
  • 18
-1

right click on project folder -> properties -> java compiler -> mark(Enable project specific settings) -> set compiler compliance level : 1.6 -> enjoy eclipse

srv
  • 1