aspose file tools*
The moose likes Beginning Java and the fly likes Return -1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Return -1" Watch "Return -1" New topic
Author

Return -1

saravanan ks
Greenhorn

Joined: Jul 10, 2003
Posts: 9
Hi friends,

I learning java thro net, in some of samples I have seen peoples using "return -1" and "return 1" in if loops.
Can one clarify what it means please
Thanks
Saravanan
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I'm not sure what an "if loop" is.

Please post the code you saw and we'll try to help you.


Mike Gershman
SCJP 1.4, SCWCD in process
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's a common problem for methods to indicate success or failure along with a return value. If you look at String.indexOf() it returns non-negative for a successful find and -1 for not found. And if you look at BufferedReader.readLine() it returns a valid string for good reads and null for end of file. So right from the JDK we have two solutions that are not consistent.

Yikes! How many more ways will we find to indicate some special condition? Quite a few, I'm afraid.

I'd suggest two lessons from this: read the JavaDoc closely on methods you're not familiar with, and try to follow some example in your own methods and document them well so they'll make sense to others. These two examples won't always work ... some times negative answers are valid, sometimes null strings are valid. Then I guess you could look into throwing exceptions.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return -1