简体   繁体   English

PrintStream 类型中的方法 println(boolean) 不适用于参数 (void)

[英]The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

I am getting an error when I try executing the following code:当我尝试执行以下代码时出现错误:

package Abc;

public class Class3 {

    public void another() {
        System.out.println("Hello World");
    }

    public static void main(String[] args) {
        Class3 obj1 = new Class3();
        System.out.println(obj1.another());
    }

}

The error is:错误是:

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

Your another() function return type is 'void' which essentially says it is defined to return nothing.您的 another() 函数返回类型是“void”,它本质上表示它被定义为不返回任何内容。

package Abc;

public class Class3 {
    public void another() {
       System.out.println("Hello World");
    }

   public static void main(String[] args) {
    Class3 obj1 = new Class3();
    obj1.another();
    }

}

Println() function expect something while your method doesn't return anything. Println() 函数期待一些东西,而你的方法不返回任何东西。 That's why you are getting error.这就是为什么你会出错。

Your another method has the return type "void" so basically it doesn't return anything.您的另一种方法具有返回类型“void”,因此基本上它不返回任何内容。 So you can't Print anything .If you want your code to work you just called obj1.another().所以你不能打印任何东西。如果你想让你的代码工作,你只需调用 obj1.another()。 Whitout the System.out.println() method.除了 System.out.println() 方法。

We can call any function in System.out.println(boolean) which returns any Object, String, int, boolean, char, char[], double, float, long value.我们可以调用System.out.println(boolean) 中的任何函数,它返回任何 Object、String、int、boolean、char、char[]、double、float、long 值。

The method println(boolean) in the type PrintStream is not applicable for any function which have void return type. PrintStream 类型中的 println(boolean) 方法不适用于任何具有 void 返回类型的函数。

package Abc;

public class Class3 {
 public String another(){
     return "Hello World";



 }
    public static void main(String[] args) {
        Class3 obj1 = new Class3();
        System.out.println(obj1.another());

    }

}

it will work because it returns String type value not void.它会起作用,因为它返回 String 类型的值而不是 void。

you want to print the string ("Hello World")?你想打印字符串(“Hello World”)? you can use the IDE tools to help you solve the problem easily;您可以使用IDE工具来帮助您轻松解决问题; you can not print twice ,you need return.你不能打印两次,你需要退货。 change like this像这样改变

 package Abc; public class Class3 { public String another(){ return "Hello World"; } public static void main(String[] args) { Class3 obj1 = new Class3(); System.out.println(obj1.another()); } }

package Abc;

public class Class3 {
    public static void another(){
        System.out.println("Hello World!");
    }
    public static void main(String[] args) {
        another();
    }
}

That's all you have to do, I don't even know how this was running without the another() is static.这就是你所要做的,我什至不知道如果没有another()是静态的,它是如何运行的。

Its just a feature of jdk 1.8 (Not a big issue) In order remove this error from your project just degrade your jdk from 1.8 to 1.7 it will start behaving normally.它只是 jdk 1.8 的一个特性(不是大问题)为了从您的项目中消除这个错误,只需将您的 jdk 从 1.8 降级到 1.7,它就会开始正常运行。

Steps : 1. Right click on project/Repository 2. Click on properties 3. Click Java Compiler 4. Choose jdk 1.7 from drop down 5. Click Apply & Close button步骤: 1. 右键单击​​项目/存储库 2. 单击属性 3. 单击 Java 编译器 4. 从下拉列表中选择 jdk 1.7 5. 单击应用并关闭按钮

You are done, It will rebuild the project and you are good to go.你完成了,它会重建项目,你很高兴。 Thanks.谢谢。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PrintStream 类型中的方法 println(double) 不适用于 arguments (String, double) - The method println(double) in the type PrintStream is not applicable for the arguments (String, double) 类型PrintStream的方法printf(String,Object ...)不适用于参数(String,void) - The method printf(String, Object…) in the type PrintStream is not applicable for the arguments (String, void) PrintStream 类型中的方法 println(int) 不适用于参数 (String, int, int, int) - The method println(int) in the type PrintStream is not applicable for the arguments (String, int, int, int) JspWriter类型的方法print(boolean)不适用于参数(void) - The method print(boolean) in the type JspWriter is not applicable for the arguments (void) PrintStream 类型中的 printf(String, Object[]) 方法不适用于参数 (...) - The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (...) ArrayList 类型中的 toArray(T[]) 方法<Boolean>不适用于参数 (boolean[]) - The method toArray(T[]) in the type ArrayList<Boolean> is not applicable for the arguments (boolean[]) 错误:找不到适合 println(String,float) 的方法? 方法 PrintStream.println(float) 不适用? - error: no suitable method found for println(String,float)? method PrintStream.println(float) is not applicable? 减少代码:“ VoteService类型中的方法changeVote不适用于参数(无效)” - Cutting down code: “The method changeVote in the type VoteService is not applicable for the arguments (void)” 声明类型中的方法assertEquals(Object,object)不适用于参数(String,Void) - Method assertEquals(Object, object) in the type Assert is not applicable for the arguments (String, Void) Integer 类型中的方法 parseInt(String) 不适用于参数(布尔值) - The method parseInt(String) in the type Integer is not applicable for the arguments (boolean)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM