0

I just wanted to test something with Javas printf method. Now its been a while since I last used this, so maybe this is now normal behaviour.

This code is an example taken from http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html and Wikipedia.

The thing is, it wont run.

import java.util.Calendar;

public class TestPrintf {

    public static void main(String[] args) {

            System.out.printf("%s, %s", "Hello", "World!");

           // Writes a formatted string to System.out.
           System.out.format("Local time: %tT", Calendar.getInstance());
           // -> "Local time: 13:34:18"


    }

}

Leads to

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method printf(Locale, String, Object[]) in the type PrintStream is not applicable for the arguments (String, String, String)
The method format(String, Object[]) in the type PrintStream is not applicable for the arguments (String, Calendar)

at TestPrintf.main(TestPrintf.java:7)

I am using Eclipse Version: Mars.2 Release (4.5.2) on Ubuntu with Java version 1.8.0_74.

I know it used to work this way, but now I have to supply an array with the variables? What if I want to format a mix of strings and integers? An Object[] with mixed types cant really be required now can it?

Some insight why this is required/was changed would be appreciated.

voiDnyx
  • 975
  • 1
  • 11
  • 24
  • 1
    Check the Java compiler version and source compatibility you have set... Basically Java is not able to recognize varargs in your case... – Codebender Mar 15 '16 at 14:54

1 Answers1

1

I would suggest you to check that the compiler compliance level is set to 1.5 or higher in your project.

See this question for an explanation on how to do this.

Community
  • 1
  • 1
Diyarbakir
  • 1,999
  • 18
  • 36
  • Thanks, even though it WAS set to 1.8 and I definitely didnt change a setting, it now works after I checked the compiler compliance level (where it 100% already said 1.8) and hit apply. – voiDnyx Mar 16 '16 at 10:12