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;
4
  • why static ? remove this, it doesn´t give you any advantage... Commented Jun 22, 2017 at 18:40
  • Starting with JDK7 you can do something like that and it will just translate it to an if/elseif block with String.equals(). What version are you using? Commented Jun 22, 2017 at 18:40
  • Your question is unclear - can you give an example of input and output for that method? Commented Jun 22, 2017 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 Commented Jun 23, 2017 at 6:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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
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.
if I'm doing case activities[0] : I'm getting constant expression required error, although activities[0] returns String
Its only allowing "somevalue" as case:
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.
|
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.

3 Comments

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
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
Then run a loop on your activities array and compare each value of activities array with the value of activity string through equalsIgnoreCase method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.