0

I've been using Eclipse to learn Java and have run into an issue trying to use printf. I keep getting the error message: "The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)"

Here's where I'm seeing a problem:

public class Lesson25 {
    public static void main(String[] args) {
        System.out.printf("Total: %d & Quantity: %d\n", 5, 120);
    }
}

and

public class Lesson10 {
    public static void main(String[] args) {

        for (int i=0; i<5; i++) {
            System.out.println("The value of i is: " + i);
        }
        for (int x=5; x>0; x--) {
            System.out.printf("The value of n is: %d\n", x);
        }
    }
}

Can anyone help me figure out what's wrong? Thanks

ntalbs
  • 28,700
  • 8
  • 66
  • 83
Ishy N
  • 21
  • 6
  • Exactly which line of code is throwing an error? Please mention that line too because `System.out.printf("Total: %d & Quantity: %d\n", 5, 120);` does not give an error – mustangDC Jul 23 '15 at 05:27
  • 2
    Your code compiles with Java 8 on my machine. Make sure your Eclipse is using Java 8. http://stackoverflow.com/a/13636584/2646526 – heenenee Jul 23 '15 at 05:32
  • This should be valid in earlier versions of Java too. Looks to me like the configuration is just screwed up. – ajb Jul 23 '15 at 05:34
  • This doesn't happen to me either! – Sweeper Jul 23 '15 at 05:48
  • Please right-click on your project, select "Build Path", "Configure Build Path", and the Libraries tab, and tell us what's there. That might help us see if you're using a really bogus JRE. – ajb Jul 23 '15 at 05:55
  • The issue shows up because of my use of printf (the red error line is under printf). I've changed my compiler compliance level from 1.4 to 1.7 as Stephan Herrmann suggest but am still seeing an error. I checked my "User Libraries" tab and nothing seems to be there...haha. Under "Build Path", the JRE library being used is "JRE Container". – Ishy N Jul 23 '15 at 13:21
  • When selecting compliance 1.7 and not having a 1.7 JRE you should see a warning on the `Java Compiler` page. In case the warning *is* shown: when you "Edit..." `Java Build Path > Libraries > JRE System Libraries`, what options do you see? Can you select a `Execution environment` named `JavaSE-1.7`. In parantheses it should show which actual JRE is used. When all matches up nicely at 1.7 and after rebuilding the project the error should be gone. – Stephan Herrmann Jul 23 '15 at 17:49
  • Not sure I know what you mean, Stephan. There's no `JRE System Libraries` tab under `Java Build Path`. Only `User Libraries` which is empty because there are no defined user libraries. I can go to the `Execution Environment` under `Installed JREs` which is where I find `JavaSE-1.7` under `Execution Environments` and can click it to make it appear under `Compatible JREs` but after that I'm not sure what to do. Clicking `OK` doesn't change the fact that I can't use printf. – Ishy N Jul 23 '15 at 19:48

1 Answers1

0

It's the compiler's compliance level that needs to be configured to use the desired version of Java. Find this option in Preferences > Java Compiler.

If [x] Use compliance from execution environment ... is checked, then follow the link Java Build Path to configure the JRE / execution environment.

Your examples are accepted with any compliance greater or equal 1.5 (below 1.5 varargs method are not supported, hence explicit mention of Object[]).

Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38