Java && 错误“对于参数类型 java.lang.String,java.lang.String,运算符 && 未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24529248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:45:36  来源:igfitidea点击:

Java && Error "The operator && is undefined for the argument type(s) java.lang.String, java.lang.String"

java

提问by Gladson Robinson

 if(pro_name.getText() && pro_price.getText() && pro_count.getText())
 {
 }

Am getting an error in eclipse java

在 eclipse java 中出现错误

The operator && is undefined for the argument type(s) java.lang.String, java.lang.String

运算符 && 未定义参数类型 java.lang.String, java.lang.String

回答by Joop Eggen

if (!pro_name.getText().isEmpty()
    && !pro_price.getText().isEmpty()
    && !pro_count.getText().isEmpty())

Conditions strictly require a boolean expression in java.

条件严格要求在 java 中使用布尔表达式。

回答by Ankit Lamba

here getText()returns String, and in java &&operator is only defined for booleannot for String.

这里getText()返回String,而在 java&&操作符中只定义了booleannot for String

This is why eclipse is showing this error.

这就是 eclipse 显示此错误的原因。

回答by confuse

You can only use && (AND) operator for booleans. .getText() returns a string, which is not a boolean values. You need to do a checkup that returns a boolean to do this, for example:

您只能对布尔值使用 && (AND) 运算符。.getText() 返回一个字符串,它不是布尔值。您需要执行返回布尔值的检查才能执行此操作,例如:

if(!pro_name.getText().isEmpty() ...)

I.e., if the answer from getText() is not null it will be translated to true. and tjhe && comparison will work.

即,如果 getText() 的答案不为 null,它将被转换为 true。并且 tjhe && 比较将起作用。

A tip is to set a variable to the answer from getText() so you can reuse it, instead of later (I am asuming) you get the text again. I.e.:

一个提示是为 getText() 的答案设置一个变量,以便您可以重用它,而不是稍后(我假设)您再次获得文本。IE:

var pro_name_result = pro_name.getText();
if(!pro_name_result.isEmpty() ...) {

回答by sayan

&& operator is valid for checking boolean values

&& 运算符可用于检查布尔值

if(pro_name.getText()=="abc" && pro_price.getText().isEmpty() && pro_count.getText().equals("mango")){   }

above one is sample one...this wont produce compilation errors.

上面一个是示例一...这不会产生编译错误。

isEmpty(), equals(), equalsIgnoreCase(),contains()==> these are the permissible operations on strings, each of them returns boolean values( trueor false)

isEmpty(), equals(), equalsIgnoreCase(), contains()==> 这些是允许的字符串操作,每个操作都返回布尔值(truefalse

==this checks for equality and hence returns boolean true or false value

==这会检查相等性,因此返回布尔值 true 或 false 值