Why/when does System.out.println(); give error: method print(boolean)...not applicable

3k views Asked by At

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?

4

There are 4 answers

1
SnowmanXL On

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.

0
Prapti Joshi On

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.

0
yousef On

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);
0
Sunil Timilsina On

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