0

I want to use existing array element as switch case rather giving constant string value to switch case

I have values in resource string array that I used to display, and user has to select from these display values, now I want to compare the input value that I saved in shared preference and the values that I have in array resource, I wrote something like this but it didn't work

private static String activity;
private static int result;
activity = SharedPrefUtils.getActivityLevel(context);

String[] activities 
= context.getResources().getStringArray(R.array.activity);

switch (activity){
      //something like getting values from array
        case activities[0]:
            result = 0;
            break;
        case activities[1]:
            result = 200;
            break;
        case activities[2]:
            result = 300;
            break;
    }

    return result;
blackHawk
  • 6,047
  • 13
  • 57
  • 100
  • why `static` ? remove this, it doesn´t give you any advantage... – Opiatefuchs Jun 22 '17 at 18:40
  • Starting with JDK7 you can do [something like that](https://stackoverflow.com/questions/10240538/use-string-in-switch-case-in-java) and it will just translate it to an if/elseif block with String.equals(). What version are you using? – ZeldaZach Jun 22 '17 at 18:40
  • Your question is unclear - can you give an example of input and output for that method? – assylias Jun 22 '17 at 18:45
  • In my case i dnt want to give constant string to switch case, I want to utilize existing array that stores that string values that i want to use as switch case – blackHawk Jun 23 '17 at 06:23

2 Answers2

0

Instead of iterating through an array, you can convert it into a List and use indexOf to get the element index, e.g.:

String[] activities = context.getResources().getStringArray(R.array.activity);
List<String> activitiesList = Arrays.asList(activities);
int index = activitiesList.indexOf(activity);

This is what the javadoc says:

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

So, you can base your logic on index value.

Another approach would be to use a Map<Integer, String> and pass the Integer key to the method based on whichever value is selected by a user.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • In my case i dnt want to give constant string to switch case, I want to utilize existing array that stores that string values that i want to use as switch case – blackHawk Jun 23 '17 at 06:26
  • You can't write a switch with dynamic number of cases as in this example, number of cases will depend on number of elements. So, you should use either `indexOf` call or `for` loop. – Darshan Mehta Jun 23 '17 at 07:53
  • if I'm doing case activities[0] : I'm getting constant expression required error, although activities[0] returns String – blackHawk Jun 23 '17 at 08:01
  • Its only allowing "somevalue" as case: – blackHawk Jun 23 '17 at 08:03
  • Ah yes, you can't use any variable in `switch` case unless it's final. So, it has to be a `String` literal. We can't use `switch` case I am afraid. – Darshan Mehta Jun 23 '17 at 08:06
  • Is that limitation? It shoild be dynamic like we can use value from array dynamically in switch case – blackHawk Jun 23 '17 at 08:32
  • Have a look at switch case's documentation [here](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html). – Darshan Mehta Jun 23 '17 at 09:11
0

First of all make sure your "activity" String and the items of "activities" array are returning you some values. Try to print the values to check that. If all of them are returning you values then you can compare String in this way:

if (activity.equalsIgnoreCase(activities[0]){
   Log.e("Matched" , "Yeah, it matched");
}

You can match your all String values one by one in this way.

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11
  • Yes, but I want to have dynamic selection through switch block, like i dont have to give case as "String" and could utilize array that storing that values – blackHawk Jun 23 '17 at 06:21
  • In my case i dnt want to give constant string to switch case, I want to utilize existing array that stores that string values that i want to use as switch case – blackHawk Jun 23 '17 at 06:23
  • Then run a loop on your activities array and compare each value of activities array with the value of activity string through equalsIgnoreCase method. – Zohaib Hassan Jun 23 '17 at 16:47