Home arrow static arrow Java Programming [Archive] - leave a method out of If(){...}
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - leave a method out of If(){...}
This topic has 5 replies on 1 page.

Posts:33
Registered: 4/1/04
leave a method out of If(){...}  
Jun 27, 2004 3:27 PM



 
Hi,

I was just wondering if there is a way to leave a method before going until the end like this:

....
if(){
leave;
}else if(){
leave;
}
...continue if you didn*t leave...

I know there is return, but that just leaves the "if" and not the whole method.

 

Posts:441
Registered: 2/25/04
Re: leave a method out of If(){...}  
Jun 27, 2004 3:36 PM (reply 1 of 5)



 
Return leaves the whole method.

Pete
 

Posts:33
Registered: 4/1/04
Re: leave a method out of If(){...}  
Jun 27, 2004 4:03 PM (reply 2 of 5)



 
Ok, I just tried return and it does leave the whole method if I use it just in my little return test, but if I step through my real program, it doesn't. Guess I have some mistake somewhere and just have to find it :-)
thx
 

Posts:21,719
Registered: 98-02-20
Re: leave a method out of If(){...}  
Jun 27, 2004 5:26 PM (reply 3 of 5)



 
One or more of your "if" tests is incorrect.

Do any of them involve java.lang.Strings? Do you do the test with "=="? If so, change it to use the equals() method and you'll get results that match your intuition better.

%
 

Posts:1,888
Registered: 22/08/01
Re: leave a method out of If(){...}  
Jun 27, 2004 10:54 PM (reply 4 of 5)



 
Ok, I just tried return and it does leave the whole method if I use it just in my little return test, but if I step through my real program, it doesn't.

When you do return in a method it leaves the method. No discussion on that (Yes it possible that in a finally block some stuff happens after it but thats not really an issue here)

So it means (like Duffymo says) your logic is flawed and your real program never gets to the return.
 

Posts:33
Registered: 4/1/04
Re: leave a method out of If(){...}  
Jun 29, 2004 1:23 AM (reply 5 of 5)



 
Thx, you were right.

It was the old (String == String) mistake instead of equals().
 
This topic has 5 replies on 1 page.