Eclipse Java printf issue PrintStream is not appli

2020-03-04 03:01发布

问题:

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?

回答1:

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 :)



回答2:

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



回答3:

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