Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By lime

Thread: Variable name from table?

  1. #1
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Variable name from table?

    How can I make variable from table value?

    Example:
    Java Code:
    String table [] = {"vara","varb"};
    for(int i = 0; i < table.length; i++)
    {
         int table[i] = i;
    }
    And vara = 0, varb = 1 here.

    Is this even possible?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Variable name from table?

    Standard answer: Java is a compiled language, variables don't exist during runtime. Probably what you want is to use a Map such as HashMap to be able to lookup objects through a key.

    Java Code:
    Map<Integer,String> table = new HashMap<>();
    
    table.put(0, "vara");
    table.put(1, "varb");
    
    String somevar = table.get(0);
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: Variable name from table?

    If I understand it good, somevar returns String value = "vara"? So how can this help me?

    Simplest way to make what I want is something like this:
    int vara = 0;
    int varb = 1;
    int varc = 2;
    ...
    ...


    But it doesn't make any sense.
    So I want vara, varb, varc to be saved in table. And then make int variables named with this.
    I can't understand why you are giving me something that creates string variable and takes it value from table(?)... I don't need this.
    If it so, I can just use this:

    String table[] = {"vara","varb"};
    String somevar = table[0]; // why somevar? I don't want and need this...


    And it will work fine. But I don't want to make string as variable value. I want to make string as variable name.


    So, where is my variable named vara with value of 0...?


    I tried to use what you gave me.
    Map<Integer,String> table = new HashMap<>();

    String tab[] = {"vara", "varb"};

    for(int i = 0; i < tab.length; i++)
    {
    table.put(i, tab[i]);
    int table.get(i) = i;
    }
    Can't make variable with name from this table...
    int table.get(i) = i; gives me same error...

    I see two posibilities here. You don't understand me(just gave me thing that I dont need and doesn't help me). I don't know how to use it(well, you didn't explained how can I make variable that name is taken from String value...).

    I don't what more can I tell here. I can't explain better what I think...
    Last edited by Nozz; 04-17-2014 at 06:37 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Variable name from table?

    Variable names are created by the programmer when he types in source code.
    If a name is not defined in the source by the programmer typing it in, the compiler will give an error saying that the symbol is not defined.

    The sample code gimbal2 posted is a standard way to associate a name with a value at execution time.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: Variable name from table?

    Map<Integer,String> table = new HashMap<>();

    table.put(0, "vara");
    table.put(1, "varb");

    String somevar = table.get(0);
    So, if its standard way, how can I get it other way?
    Instead of:
    String somevar = table.get(0);
    Something like:
    int somevar = table.get("vara");

    I want somevar to return int value from those two which are associated. Can I do this or I cant?
    Its now "you give me number, I give you word". But I need "you give me word, I give you number"...

    Anyway, even if I can, table.put 100x times doesn't make any sense... And while I try to "put" everything by loop, I can't loop words associated to numbers("vara", "varb"...).

  6. #6
    lime is offline Member
    Join Date
    Apr 2014
    Posts
    14
    Rep Power
    0

    Default Re: Variable name from table?

    You can't dynamically create variables in Java, but you can try this example using map, you can acces the to your int values by String names from your array:
    String[] tab = {"vara", "varb"};
    Map<String, Integer> m = new HashMap<>();
    for(int i = 0; i < tab.length; i++) {
    m.put(tab[i], i);
    }
    System.out.println("vara is " + m.get("vara")); //prints Integer value of 0
    System.out.println("varb is " + m.get("varb")); //prints Integer value of 1
    EDIT: this solves your question "int somevar = table.get("vara");"; but do this: int somevar = m.get("vara"); just reverse keys with values in the map.
    Last edited by lime; 04-17-2014 at 06:53 PM.
    Nozz likes this.

  7. #7
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 01-03-2014, 01:33 PM
  2. Replies: 2
    Last Post: 04-03-2013, 05:33 PM
  3. Replies: 3
    Last Post: 02-23-2012, 10:30 AM
  4. Replies: 1
    Last Post: 09-27-2011, 07:06 AM
  5. Replies: 1
    Last Post: 01-08-2010, 07:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •