3
Double new_val = 10.0;
String a = "new";
String b = "val";
Double v1 = 25.0;
Double result = 0.0;

Public void getVal() {
    //String variable c contain double variable name
    String c = a+"_"+b;
    //I want to get c's value as 10.0 as its a variable already defined
    result = v1*c;
}

"c" String value contain variable name "new_val" and that is used for further

Loki
  • 801
  • 5
  • 13
mehal.ce
  • 33
  • 1
  • 5
  • `result = v1*c;` this is not a valid semantics, as v1 is number and c is string.. – Jos Jul 23 '16 at 07:40

4 Answers4

4

If the question is, whether you can get the value of a variable knowing its name at runtime, then good news.... Yes for sure you can... you will need to do something called REFFLECTION...

which is allowing you as developer to make an instrocpection of the class and even "browse" all the info that the class is holding

in your case you need to find a "variable" (or Field) by the name and read its value...

look the doc for more info, and I would recommend you to consider if you really need to do this... normally reflection is intended to be used when you want to access info from another class and not about browsing yourself...

you can maybe redesign a little the application and define some constants and methods so other can see what you are exposing to them and making for them available...

Example:

public class Jung {
Double new_val = 10.0;
String a = "new";
String b = "val";
Double v1 = 25.0;
Double result = 0.0;

public void getVal() {
    // String variable c contain double variable name
    String c = a + "_" + b;
    Double cAsVal = 0.0;
    try {
        cAsVal = dale(c);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    result = v1.doubleValue() * cAsVal.doubleValue();
    System.out.println(result);
}

public static void main(String[] args) {
    Jung j = new Jung();
    j.getVal();
}

public Double dale(String c)
    throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    Field field = this.getClass().getDeclaredField(c);
    field.setAccessible(true);
    Object value = field.get(this);
    return (Double) value;
}
}
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

What is you question?

If you want to do "25*c", it's like mutipling a Double (25) with a String ("new_val"). It wont work

Souin
  • 94
  • 1
  • 11
  • i want to convert string value("c") to variable name (Double new_val) – mehal.ce Jul 23 '16 at 07:36
  • 1
    still not clear.. do you want to create a variable with a name that is in a String value? – Jos Jul 23 '16 at 07:39
  • You can't. How do you want to convert a String in Double? For example, how can you convert the String str = "myString" to a Double? The other direction works. Indeed, id you want to convert a Double dbl = 28 to a String, you can use String.valueOf(dbl) . It will return a String str2 = "28" – Souin Jul 23 '16 at 07:40
1

If I am correct you cant have variable name in code based on another variable value in Java. If you need to have string value in variable you should create a class with fields, for example:

class myVariable{
    String name;
    int value;
}
hal
  • 831
  • 3
  • 13
  • 32
1

i want to convert string value("c") to variable name (Double new_val)

It's great to see what reflection can be used to get the job done.

For your case, Map can do the trick for you too.

public static void main(String args[]){
    HashMap<String, Double> map = new HashMap<String, Double>();
    map.put("new_val", 10.0);

    String a = "new";
    String b = "val";
    Double v1 = 25.0;
    Double result = 0.0;

    //String variable c contain double variable name
    String c = a+"_"+b;
    //String variable c used for calculation
    result = v1 * map.get(c);

    System.out.println(result);
}

Check To use a string value as a variable name for more details.

Community
  • 1
  • 1
Eugene
  • 10,627
  • 5
  • 49
  • 67