Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Using a string to define which variable to initialize?

  1. #1
    Kami is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Using a string to define which variable to initialize?

    I am very new to java so excuse me if I am asking the obvious.

    How can I get a variable name from a string? What I mean is, if I have the int value = "31415926", the String intName = "int2", and a bunch of ints named int1, int2, etc., how can I from that make it so int2 = 31415926? (If that makes any sense.)

    An if statement for every single possibility would be impractical to say the least.

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Using a string to define which variable to initialize?

    Well if I understand you correctly then you can't do that.

    You can't use the value of a String to determine the name of a new variable.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Using a string to define which variable to initialize?

    Variables don't have the same role in Java as they do in PHP for example or other langauges where they really do function as names in the sense that you can summon their values up later based on the value of a string (user input etc).

    If you really want to associate int values with names, consider using a Map<String,Integer>. But, more likely, there is some other way of doing whatever it is that you are trying to do.
    Solarsonic likes this.

  4. #4
    Kami is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Using a string to define which variable to initialize?

    Thank you for the reply.

    Could you explain that further?

    I assume that if this is not possible, then you also can not do the opposite(store the name of an input variable in a string) either?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Using a string to define which variable to initialize?

    Quote Originally Posted by Kami View Post
    Thank you for the reply.

    Could you explain that further?

    I assume that if this is not possible, then you also can not do the opposite(store the name of an input variable in a string) either?
    If I have a variable foo I can make a corresponding string literal out of it with quote marks: "foo". But, as noted, Java variables are not (and do not have) names as in other languages. foo is an expression with an int value, and "foo" is an expression with a string value and there is no essential connection between the two. Whatever it is that you are trying to do can probably better be done some other way.

Similar Threads

  1. Replies: 3
    Last Post: 11-23-2010, 02:19 PM
  2. Initialize a Variable
    By lala in forum New To Java
    Replies: 13
    Last Post: 11-16-2010, 06:51 PM
  3. Replies: 3
    Last Post: 09-29-2010, 09:26 AM
  4. define a variable
    By jperson in forum New To Java
    Replies: 2
    Last Post: 01-29-2010, 02:33 AM
  5. Replies: 3
    Last Post: 11-17-2008, 03:49 PM

Posting Permissions

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