wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Method similar to decode function in Oracle SQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method similar to decode function in Oracle SQL" Watch "Method similar to decode function in Oracle SQL" New topic
Author

Method similar to decode function in Oracle SQL

Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
I am looking for methos similar to decode function in Oracle SQL.
What I want to do is,
int i =0;
if (S.indexOf("P")= -1)
i = 10000
else
i = S.indexOf("P")
I will like to do it in one line instead like,
int i= decode(S.indexOf("P"),-1,10000)
Is there any such method in any class that I can use here?
Thanks!!
[ July 31, 2002: Message edited by: Gemini Moses ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Umm, can you not just write your own static method and put it in a utility class somewhere?
If you can't do that, you could use the "ternary" or "query-colon" operator:
int i = S.indexOf("P")==-1 ? 10000 : S.indexOf("P");
But note that this will call the (potentially slow) "indexOf" method twice, whereas:

will only call it once.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method similar to decode function in Oracle SQL