-1

I am trying to minimise below 10 switch case but after trying few times I am not able to. Can somebody suggest what should I do?

Thanks in advance.

 private int getCode(String digit) {

    int keycode = 0;

    switch (digit) {
        case "0":
            keycode = AndroidKeyCode.KEYCODE_0;
        break;

        case "1":
            keycode = AndroidKeyCode.KEYCODE_1;
        break;

        case "2":
            keycode = AndroidKeyCode.KEYCODE_2;
        break;

        case "3":
            keycode = AndroidKeyCode.KEYCODE_3;
        break;
    }

    return keycode;
}

Thanks

sgamer
  • 73
  • 1
  • 4
  • 13
  • This post also has a nice way: https://codereview.stackexchange.com/questions/74743/long-switch-statement-to-lookup-fifty-something-commands – tima Aug 24 '17 at 16:11
  • 1
    I'm not sure that duplicate question has the exact answer the OP is looking for, namely `return (int)digit.charAt(0)-'0'+AndroidKeyCode.KEYCODE_0;` (after checking for null and garbage inputs, of course). – Ray Toal Aug 24 '17 at 16:16

1 Answers1

-2

Use reflection API. example If you are using proguard you must write proguard rules too or disable proguard.

Nikola Hristov
  • 686
  • 1
  • 6
  • 11