dsuma
dsuma

Reputation: 1010

Java, While with multiple condition using or

I have this while statement. which should exit when there is a whitespace or a ; as a char. however it does not exit the loop when either of those conditions are true; when i use a && it works better but now expects (obviously) both conditions to be true. which still does not help me.

    while ( !pt.get(locCursor).equals(';') || !pt.get(locCursor).equals(' ')){
        word = word + pt.get(locCursor);
        if (locCursor < pt.size()-1){
            locCursor ++;
        }else{
            break;
        }

    }

Upvotes: 0

Views: 94

Answers (2)

Pshemo
Pshemo

Reputation: 124225

Take a look at your condition. If result of pt.get(locCursor) is ; then it is not space so second condition is true making entire condition true. If result is space then it is not ; and again, entire condition is true.

Instead of

!egual(';') OR !equal(' ')

use

!( equal(';') OR equal(' ') ) 

or

!egual(';') AND !equal(' ')

Upvotes: 6

Wald
Wald

Reputation: 1091

Basiclly what you have is :

While (!x || !y) in OR ( || notation ) the truth table is as it follows

x y r
0 0 0
1 0 1
0 1 1

1 1 1

Since you have invertion of the result ( ! infront both x and y ) your table is true only if you have both of them at 0.

Currently you are checking if the char is not equal to ';' or ' ' ( any char diff from one of those will break the while ).

I suppose you want to remove both of the '!' in the while.

Upvotes: 0

Related Questions