Results 1 to 8 of 8
- 10-12-2009, 10:22 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 24
- Rep Power
- 0
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;
- 10-12-2009, 10:48 PM #2
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
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.
- 10-12-2009, 10:51 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 24
- Rep Power
- 0
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.
- 10-12-2009, 10:54 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
- 10-12-2009, 11:01 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 24
- Rep Power
- 0
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.
- 10-12-2009, 11:10 PM #6
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
- 10-12-2009, 11:12 PM #7
Member
- Join Date
- Jan 2009
- Posts
- 24
- Rep Power
- 0
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...
This part has been coded already and works fine.Are you having trouble taking the "2+3" input and converting that into 5, or have you already coded that?
- 10-12-2009, 11:16 PM #8
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
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
-
how to get next character/string
By doha786 in forum New To JavaReplies: 3Last Post: 03-28-2009, 04:04 AM -
how to Parse int to a string variable
By raj reddy in forum Java ServletReplies: 10Last Post: 01-09-2009, 07:41 PM -
Inserting string with slash character into database
By Java Tip in forum Java TipReplies: 0Last Post: 02-07-2008, 08:57 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM -
String Variable
By Eric in forum Advanced JavaReplies: 1Last Post: 06-06-2007, 04:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks