为什么/何时会出现错误:method print(boolean)…not applicable

huangapple go评论112阅读模式
英文:

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

问题

我最近了解到使用 System.out.print(); 会导致这个错误:

在 PrintStream 类型中,方法 print(boolean) 不适用于参数 ()

我是在帮助别人解决编码问题时发现的这个问题。当然,自然而然的问题是:为什么他们在代码中使用了这个没有参数的语句。在文档中,我没有找到关于这种情况的错误提及。

在查阅文档时,我发现 System.out.print(T t) 对许多类型都有定义,但当完全没有参数时,它会默认为 print​(boolean x)参考链接

然后,当然会报错,因为该参数不适用。

我帮助的那个人是一个新生,之前经常使用 System.out.println(),对为什么会出现这个错误感到困惑。

最终,这位学生希望在那里放置一个字符串,而这个方法只是一个占位符。我解释说编译需要某种形式的参数。

我的问题是:为什么编译器会做出这种假设,从而产生这个错误?

英文:

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?

答案1

得分: 3

我认为控制台截断了整个消息,当我尝试在Intellij中编译System.out.print()时,它显示了所有消息:

没有找到适合的方法来打印(无参数)
方法java.io.PrintStream.print(boolean)不适用
  (实际和形式参数列表的长度不同)
方法java.io.PrintStream.print(char)不适用
  (实际和形式参数列表的长度不同)
方法java.io.PrintStream.print(int)不适用
  (实际和形式参数列表的长度不同)
方法java.io.PrintStream.print(long)不适用
  (实际和形式参数列表的长度不同)
方法java.io.PrintStream.print(float)不适用
  (实际和形式参数列表的长度不同)
方法java.io.PrintStream.print(double)不适用
  (实际和形式参数列表的长度不同)
方法java.io.PrintStream.print(char[])不适用
  (实际和形式参数列表的长度不同)
方法java.io.PrintStream.print(java.lang.String)不适用
  (实际和形式参数列表的长度不同)
方法java.io.PrintStream.print(java.lang.Object)不适用
  (实际和形式参数列表的长度不同)

编译器试图按照它们在文件中被声明的顺序来匹配所有方法。

英文:

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.

答案2

得分: 2

我遇到了相同的错误,并且我是这样解决的:

原来是这样的:

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

将它改为:

 System.out.println("x="+ x);
英文:

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

答案3

得分: 1

每当在打印语句中使用像%.2f这样的格式化方法,都请使用System.out.printf("Price:%,2f:",price),而不是System.out.println("Price:%,2f:",price)。错误会得到解决。[Price:%,2f:,price]只是在您的打印语句中使用的格式化语句的示例。

英文:

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.

答案4

得分: 1

我在尝试像这样格式化字符串时遇到了相同的错误:

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

其中 In 都是整数。后来我意识到,println() 方法不接受两个参数。它只接受一个 String 参数。

我通过以下方式纠正了这个错误:

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

函数 printf() 接受两个参数并格式化字符串。

错误看起来像这样:

Error:
|  找不到适用的方法以调用 println(java.lang.String,int,int,int)
|      方法 java.io.PrintStream.println() 不适用
|        (实际参数和形式参数列表长度不同)
|      方法 java.io.PrintStream.println(boolean) 不适用
|        (实际参数和形式参数列表长度不同)
|      方法 java.io.PrintStream.println(char) 不适用
|        (实际参数和形式参数列表长度不同)
|      方法 java.io.PrintStream.println(int) 不适用
|        (实际参数和形式参数列表长度不同)
|      方法 java.io.PrintStream.println(long) 不适用
|        (实际参数和形式参数列表长度不同)
|      方法 java.io.PrintStream.println(float) 不适用
|        (实际参数和形式参数列表长度不同)
|      方法 java.io.PrintStream.println(double) 不适用
|        (实际参数和形式参数列表长度不同)
|      方法 java.io.PrintStream.println(char[]) 不适用
|        (实际参数和形式参数列表长度不同)
|      方法 java.io.PrintStream.println(java.lang.String) 不适用
|        (实际参数和形式参数列表长度不同)
|      方法 java.io.PrintStream.println(java.lang.Object) 不适用
|        (实际参数和形式参数列表长度不同)
|  System.out.println("%d %d %d", x, y, z);
英文:

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

huangapple
  • 本文由 发表于 2020年10月7日 21:57:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64245646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定