if conditional each condition works on own but not together

Go To StackoverFlow.com

0

I tested each statement on its own and it works, but when I use || it ignores the !driver.getText().toString().equals("")part of the statement. Any ideas?

 if((!driver.getText().toString().equals(""))|| (canDrive>=0) )  
2012-04-04 01:52
by Aaron
You can make this a little cleaner by using String.isEmpty() instead of equals("")ulmangt 2012-04-04 01:54
The LHS will always be evaluated - are you sure this is the actual code - Paul Bellora 2012-04-04 01:57
What do you mean it "ignores the !driver.getText().toString().equals("")" part - Kirk Woll 2012-04-04 01:58
When run the condition is ignore - Aaron 2012-04-04 01:59
.isEmpty() is causing an erro - Aaron 2012-04-04 02:01


1

The || operator means or - so if either condition is true, the test succeeds. If you want to both conditions to be true, use &&.

More reading: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

2012-04-04 02:01
by Paul Bellora
Thanks, I feel like an idiot - Aaron 2012-04-04 02:03
@Aaron - No problem. The link is to the Java Tutorials, a great resource in general for beginning Java - Paul Bellora 2012-04-04 02:04


0

If you want to perform both parts of the condition use '|' operator. I.e.

if(driver.getText().toString().length() > 0 | canDrive >= 0) 

Though, the left part of the statement should be executed always: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.24

2012-04-04 01:54
by Eugene Retunsky
The OP is alleging that the first part is not being run, not the second - Kirk Woll 2012-04-04 01:58


0

If compiler found canDrive >=0 than defiitely it will ignore rest of part of condition because you are using || (or) condition so this will happened,

other wise perfect,

i have tried see the code.

class test{
    public static void main(String[] arr){
    String str = TestString";
    int canDrive = 1;
    if(!str.toString().equals("") || canDrive >=0)
        System.out.println("pass");
        else
        System.out.println("fail");
    }
}
2012-04-04 02:01
by Yogesh Prajapati