1

I recently learned that use of System.out.print(); results in this error:

The method print(boolean) in the type PrintStream is not applicable for the arguments ()

I found this when helping somebody else with their coding. Of course the natural question is: why do they have that in their code with no arguments. I did not find any mention of the error for this case in documentation.

Searching the documentation I found that System.out.print(T t) is defined for many types, but when no argument at all is present, it defaults to print​(boolean x). Reference.

Then, of course it complains that the argument is not applicable.

The person I was helping is a new student who had frequently used System.out.println() and was baffled as to why this error was occurring.

The student eventually wanted a string there and the method was just a placeholder. I explained an argument of some sort is required for compilation.

My question is: Why does the compiler make this assumption and thus give this error?

K. hervey
  • 119
  • 2
  • 13
  • 7
    When I try this I get an error that says `print(boolean) is not applicable` and goes on to list all the other overloads that are also not applicable. It doesn't assume or default to `print(boolean)` at all. – khelwood Oct 07 '20 at 13:59
  • 4
    `print(boolean)` just happens to be the lexicographically first method I would assume. Notice also that the `print()` methods *all* need an argument, while the `println()`-methods have a no-arg overload. – Lino Oct 07 '20 at 14:01
  • @OneCricketeer I hope I did not say that print defaults to println. Did it look that way? – K. hervey Oct 07 '20 at 14:13
  • 1
    "when no argument at all is present, it defaults to println​(boolean x)" – OneCricketeer Oct 07 '20 at 14:14
  • [Related](https://stackoverflow.com/questions/18898989/dont-understand-error-message) – Lino Oct 07 '20 at 14:14
  • @OneCricketeer Thanks. I edited to fix. I suppose I am too accustomed to typing System.out.println – K. hervey Oct 07 '20 at 14:16

4 Answers4

3

I think the console cut off the whole message, when I try to compile System.out.print() in Intellij it shows all messages:

no suitable method found for print(no arguments)
method java.io.PrintStream.print(boolean) is not applicable
  (actual and formal argument lists differ in length)
method java.io.PrintStream.print(char) is not applicable
  (actual and formal argument lists differ in length)
method java.io.PrintStream.print(int) is not applicable
  (actual and formal argument lists differ in length)
method java.io.PrintStream.print(long) is not applicable
  (actual and formal argument lists differ in length)
method java.io.PrintStream.print(float) is not applicable
  (actual and formal argument lists differ in length)
method java.io.PrintStream.print(double) is not applicable
  (actual and formal argument lists differ in length)
method java.io.PrintStream.print(char[]) is not applicable
  (actual and formal argument lists differ in length)
method java.io.PrintStream.print(java.lang.String) is not applicable
  (actual and formal argument lists differ in length)
method java.io.PrintStream.print(java.lang.Object) is not applicable
  (actual and formal argument lists differ in length)

The compiler tries to match all methods from the order in which they happen to be declared in the file.

SnowmanXL
  • 724
  • 5
  • 14
  • Thanks. I was using Eclipse. I opened up InteliJ and obtained your same result. My reason for posting the question was mainly for new learners such as the student I mentioned. Internet searching would not have helped the student at all. Sure, the Oracle doc explains, but as with so many other questions, it needed an answer in SO or elsewhere. – K. hervey Oct 07 '20 at 17:32
2

I have the same error, and I solve it like this:

it was

 System.out.println("x=", x);

change it to :

 System.out.println("x="+ x);
yousef
  • 21
  • 2
1

Whenever using a formatting method like %.2f or anything else like this in the print statement use System.out.printf("Price:%,2f:,price) instead of System.out.println("Price:%,2f:,price). The error gets resolved. [Price:%,2f:,price] is just an example of the formatting statement used in your print statement.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1

I had the same error while I was trying to format String like this:

System.out.println("%d %d \n", I, n);

Where I and the n are both integers. Later I realize, println() method does not take two arguments. That takes only one String argument. I corrected that mistakes by doing this;

System.out.printf("%d %d \n", I, n);

The function printf() takes two arguments and formats the string.

The error is looked like this:

Error:
|  no suitable method found for println(java.lang.String,int,int,int)
|      method java.io.PrintStream.println() is not applicable
|        (actual and formal argument lists differ in length)
|      method java.io.PrintStream.println(boolean) is not applicable
|        (actual and formal argument lists differ in length)
|      method java.io.PrintStream.println(char) is not applicable
|        (actual and formal argument lists differ in length)
|      method java.io.PrintStream.println(int) is not applicable
|        (actual and formal argument lists differ in length)
|      method java.io.PrintStream.println(long) is not applicable
|        (actual and formal argument lists differ in length)
|      method java.io.PrintStream.println(float) is not applicable
|        (actual and formal argument lists differ in length)
|      method java.io.PrintStream.println(double) is not applicable
|        (actual and formal argument lists differ in length)
|      method java.io.PrintStream.println(char[]) is not applicable
|        (actual and formal argument lists differ in length)
|      method java.io.PrintStream.println(java.lang.String) is not applicable
|        (actual and formal argument lists differ in length)
|      method java.io.PrintStream.println(java.lang.Object) is not applicable
|        (actual and formal argument lists differ in length)
|  System.out.println("%d %d %d", x, y, z);