Problem with IF clause - an OR clause inside does not work.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Emil_Persson
Posted On:   Monday, March 1, 2004 01:47 AM

Having two tests to make on a String item inside an array to get an IF clause execute I wrote this code: if(!textIn[i].equals(" ") || !textIn[i].equals("")){ //do something I want } However this did not work. I had to rewrite the code like this: if(!textIn[i].equals(" ")){ if(!textIn[i].equals("")){ //do something I want } } Now this looks awkward to me - why does't the first clause work? /regards Emil    More>>

Having two tests to make on a String item inside an array
to get an IF clause execute I wrote this code:


			
if(!textIn[i].equals(" ") || !textIn[i].equals("")){
//do something I want
}


However this did not work. I had to rewrite the code like


this:


			
if(!textIn[i].equals(" ")){
if(!textIn[i].equals("")){
//do something I want
}
}


Now this looks awkward to me - why does't the first clause work?



/regards Emil

   <<Less

Re: Problem with IF clause - an OR clause inside does not work.

Posted By:   Anonymous  
Posted On:   Monday, March 1, 2004 04:55 AM

BTW: you should write "".equals(textIn[i]) instead of textIn[i].equals(""). If textIn[i] is null your version would throw a NullPointerException. Just a hint ;-)

Re: Problem with IF clause - an OR clause inside does not work.

Posted By:   Axel_Richter  
Posted On:   Monday, March 1, 2004 02:26 AM

Hi,




if(!textIn[i].equals(" ") || !textIn[i].equals("")){
//do something I want
}

A logical linkage of OR is TRUE if ONE condition is fulfilled. Either your string is NOT " " OR your string is NOT "". Let textIn[i] be "", then !textIn[i].equals(" ") is 1 and the whole condition is fulfilled.



In the second case it is first examined whether your string is NOT " ". If so, then examined whether it is NOT "". That corresponds to a AND linkage.


if(!textIn[i].equals(" ") && !textIn[i].equals("")){
//do something I want
}



Greetings



Axel
About | Sitemap | Contact