Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method similar to decode function in Oracle SQL

 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic