Results 1 to 8 of 8
  1. #1
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default Help with creating a variable from a character in string

    So basically I am having alot of trouble here. I have a user inputted string. Whatever the second character of the string is, that will become the variable name for a type long. Any idea how to do this? I'm doing this which is infinitesimally wrong where str is the variable of the String and value is just a value of type long.
    Java Code:
    long str.charAt(1) = value;

  2. #2
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    I do not think there is a way to convert a string into a variable name in java. You might consider making a class that stores the name and value.

  3. #3
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    For example my program has to do this...spaces can be entered but I have it remove spaces within the program
    Input= "=a2+3"

    What this has to do is set a variable a = 2+3. I have the rest of the program working fine, I just need it to extract a character and use that as the name of the variable.

  4. #4
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Mayur View Post
    What this has to do is set a variable a = 2+3. I have the rest of the program working fine, I just need it to extract a character and use that as the name of the variable.
    What is the variable being used for (why does it need to have that variable name)?

  5. #5
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    The program is a simple calculator. It has to work for just expressions put in such as "2+3" and it has to also take an input as "=a2+3" where 'a' can be any letter lower or upper case and the expression gets inputted into the inputted letter.

  6. #6
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Mayur View Post
    The program is a simple calculator. It has to work for just expressions put in such as "2+3" and it has to also take an input as "=a2+3" where 'a' can be any letter lower or upper case and the expression gets inputted into the inputted letter.
    Assuming that all of the variable names are 1 letter, you could just create 26 double variables, one for each letter.

    Are you having trouble taking the "2+3" input and converting that into 5, or have you already coded that?

  7. #7
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by matzahboy View Post
    Assuming that all of the variable names are 1 letter, you could just create 26 double variables, one for each letter.
    I see what you're saying here. This is the "lazy" way I guess but it works. That would be a ton of if statements though...
    Are you having trouble taking the "2+3" input and converting that into 5, or have you already coded that?
    This part has been coded already and works fine.

  8. #8
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Mayur View Post
    I see what you're saying here. This is the "lazy" way I guess but it works. That would be a ton of if statements though...
    After some quick googling, I found out that there is a way to do this using hashmaps (though I've never personally used them before, so I don't know how well this works).

    See if this helps you: dynamic variables

Similar Threads

  1. how to get next character/string
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-28-2009, 05:04 AM
  2. how to Parse int to a string variable
    By raj reddy in forum Java Servlet
    Replies: 10
    Last Post: 01-09-2009, 08:41 PM
  3. Replies: 0
    Last Post: 02-07-2008, 09:57 AM
  4. Replies: 1
    Last Post: 08-03-2007, 11:52 AM
  5. String Variable
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2007, 05:30 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
  •