Guest User

Untitled

a guest
Aug 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. How search in java enum?
  2. public static final String CONST1="abc";
  3. public static final String CONST2="cde";
  4. public static final String CONST3="ftr";
  5. ...................................
  6. public static final String CONSTN="zya";
  7.  
  8. if (String val in [CONST1,CONST2,CONST3]) {
  9. do something;}
  10. else {
  11. ....
  12.  
  13. public class EnumTest
  14. {
  15. public static void main(final String[] args)
  16. {
  17. final Option o = Option.safeValueOf(args[0]);
  18. switch(o)
  19. {
  20. case CHOICE_A: // fall through
  21. case CHOICE_B: // fall through
  22. case CHOICE_C: // fall through
  23. System.out.format("You selected %s", o );
  24. break;
  25. case CHOICE_D:
  26. System.out.format("You selected %s", o);
  27. break;
  28. default:
  29. System.out.format("Default Choice is %s", o );
  30. }
  31. }
  32.  
  33. public enum Option
  34. {
  35. UNRECOGNIZED_CHOICE, CHOICE_A, CHOICE_B, CHOICE_C;
  36.  
  37. // this hides the Exception handling code
  38. // so you don't litter your code with try/catch blocks
  39. Option safeValueOf(final String s)
  40. {
  41. try
  42. {
  43. return Option.valueOf(s);
  44. }
  45. catch (final IllegalArgumentException e)
  46. {
  47. return UNRECOGNIZED_CHOICE;
  48. }
  49. }
  50. }
  51. }
  52.  
  53. switch (val) {
  54. case CONST1: case CONST2: case CONST3:
  55. do_something();
  56. break;
  57. case CONST4:
  58. do_something_else();
  59. break;
  60. default:
  61. // we should not be here?!?
  62. break;
  63. }
  64.  
  65. val = MyEnum.valueOf(stringValue);
Add Comment
Please, Sign In to add comment