This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Method similar to decode function in Oracle SQL
 
Similar Threads
Date difference, with the table containing null values for date
Strange IBatis Exception. Plz Help!!
decode in db2
String searching
Can I Select column A OR column B ?