0

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;
        }

    }
2
  • 5
    Your logic is off. The correct way is using logical AND, and it doesn't require "obviously" both conditions to be true. I.e. "while(not ';' and not ' ')". Commented Nov 21, 2013 at 9:54
  • how can it work "better" Commented Nov 21, 2013 at 11:19

2 Answers 2

6

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(' ')
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.