nosdalg
nosdalg

Reputation: 571

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

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

Upvotes: 1

Views: 34844

Answers (4)

sayan
sayan

Reputation: 501

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

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

Upvotes: 2

confuse
confuse

Reputation: 230

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:

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.

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.:

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

Upvotes: 1

earthmover
earthmover

Reputation: 4525

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

This is why eclipse is showing this error.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109557

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

Conditions strictly require a boolean expression in java.

Upvotes: 3

Related Questions