[英]Java && Error “The operator && is undefined for the argument type(s) java.lang.String, java.lang.String”
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定义运算符&&。
&& 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( true
or false
) isEmpty()
, equals()
, equalsIgnoreCase()
, contains()
==>这些是对字符串的允许操作,它们每个都返回布尔值( true
或false
)
==
this checks for equality and hence returns boolean true or false value ==
这会检查是否相等,因此返回布尔值true或false
if (!pro_name.getText().isEmpty()
&& !pro_price.getText().isEmpty()
&& !pro_count.getText().isEmpty())
Conditions strictly require a boolean expression in java. 条件严格要求在Java中为布尔表达式。
here getText()
returns String
, and in java &&
operator is only defined for boolean
not for String
. 这里的
getText()
返回String
,并且在java &&
运算符中仅为boolean
而不是String
定义。
This is why eclipse is showing this error. 这就是为什么eclipse显示此错误的原因。
You can only use && (AND) operator for booleans. 您只能将&&(AND)运算符用于布尔值。 .getText() returns a string, which is not a boolean values.
.getText()返回一个字符串,该字符串不是布尔值。 You need to do a checkup that returns a boolean to do this, for example:
您需要进行检查以返回一个布尔值来执行此操作,例如:
if(!pro_name.getText().isEmpty() ...)
Ie, if the answer from getText() is not null it will be translated to true. 即,如果getText()的答案不为null,它将被转换为true。 and tjhe && comparison will work.
和&&比较将起作用。
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. 提示是为getText()的答案设置一个变量,以便您可以重用它,而不是稍后(我正在假设)您可以再次获取文本。 Ie:
即:
var pro_name_result = pro_name.getText();
if(!pro_name_result.isEmpty() ...) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.