简体   繁体   中英

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

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?

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM