0
final static int 
    DRAGON = 5,
    SNAKE = 6;

String this_item = "DRAGON";
int item_value = super_func(this_item);//must be 5

Is it possible to implement this super_func, as shown above?

Or maybe I am asking for too much?

EDIT:

I am not allowed to use enum. I can:

a) use a map (as someone pointed, or 2 array lists) or
b) do this with some pro way (but it doesn't seem possible).

3
  • Is this just an idle question? Why do you need to do this? Maybe you should explain what you're trying to do that lead you to need this. Commented May 24, 2014 at 14:40
  • possibly duplicate: stackoverflow.com/questions/744226/… Commented May 24, 2014 at 14:41
  • @timgeb That is not a duplicate. Commented May 24, 2014 at 14:42

5 Answers 5

3

I don't know what this value is supposed to represent, but you could use an Enum.

enum Animal {
    DRAGON(5),
    SNAKE(6);

    private final int a;

    private Animal(int a){
        this.a = a;
    }

    public int getA(){
        return a;
    }
}

And then

String this_item = "DRAGON";
int item_value = Animal.valueOf(this_item).getA();//5
Sign up to request clarification or add additional context in comments.

Comments

2

You can get the value of a variable with Java Reflection tricks, but why not declaring a simple enum and using it:

    enum Creature{
    DRAGON(5), SNAKE(6);
     ...
    }

Comments

1

While it's technically possible to do this using reflection, I'd strongly recommend you to rethink your structure, and consider one of the alternatives that have been mentioned in the other answers.

The simplest would probably be to use a map

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public class VariableNameTest
{
    private static final Map<String,Integer> map;
    static
    {
        Map<String, Integer> m = new LinkedHashMap<String, Integer>();        
        m.put("DRAGON",5);
        m.put("SNAKE",6);
        map = Collections.unmodifiableMap(m);
    }

    public static void main(String[] args)
    {
        System.out.println(getValue("DRAGON"));
        System.out.println(getValue("SNAKE"));
        System.out.println(getValue("Boo!"));
    }

    public static int getValue(String name)
    {
        Integer i = map.get(name);
        if (i == null)
        {
            // Do some error handling here!
        }
        return i;
    }
}

But if your intention is to extend this with additional functionality, you should probably introduce a class like it was recommended in https://stackoverflow.com/a/23846279/3182664

Just for completeness: The reflection solution, not recommended!

import java.lang.reflect.Field;

public class VariableNameTest
{
    static final int DRAGON = 5;
    static final int SNAKE = 6;

    public static void main(String[] args)
    {
        System.out.println(getValue("DRAGON"));
        System.out.println(getValue("SNAKE"));
        System.out.println(getValue("Boo!"));
    }

    private static int getValue(String name)
    {
        try
        {
            Class<?> c = VariableNameTest.class;
            Field f = c.getDeclaredField(name);
            return f.getInt(null);
        }
        catch (NoSuchFieldException e)
        {
            // Many things
            e.printStackTrace();
        }
        catch (SecurityException e)
        {
            // may cause
            e.printStackTrace();
        }
        catch (IllegalArgumentException e)
        {
            // this to 
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            // fail horribly 
            e.printStackTrace();
        }
        return 0;
    }
}

5 Comments

Not recommended, because it's too complex and there are simpler alternatives, right?
Not recommended, because Many things may cause this to fail horribly - and there are simpler, less invasive solutions, yes.
He said he's not allowed to use enums
@Cobbles This "edit" of the original question was done while (or shortly after?) I referred to ZouZou's recommendation. However, I intentionally said that he might introduce a class (which does not necessarily mean that this class should extend the Enum class). Such a class could offer a functionality that is roughly similar to that of an enum. Of course, one could not switch over the instances/values of this class, but a valueOf(String) method could be offered, solving the initial issue.
@Marco13 Ah I understand, sorry for accusing you :P
1

As far as I know such a thing is not possible, but you coudl store your variables in Map

HashMap<String,Integer> map = new HashMap<>();
map.put("DRAGON",5);
map.put("SNAKE",6);

int itemValue = map.get("DRAGON"); //Sets itemValue to 5

Comments

1

You can also use a switch() statement.

int item_value;
switch(this_item) {
     case "DRAGON":
         item_value = 5;
         break;
     case "SNAKE":
         item_value = 6
         break;

or different solution doing the opposite

private String getAnimal(int itemValue) {
     switch(item_value) {
         case 5:
             return "DRAGON";
         case 6:
             return "SNAKE";
         default:
             return null;
}

Comments

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.