As far as I know, there is no way to translate a String
into a variable name in Java. What you might want to do instead, is have a mapping from String
s to int
s. Then you can parse the command and retrieve the values associated with those String
s.
For example:
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
The difference is that now instead of storing the values in a variable, you associate them with a String
which you can find in your input text.
For example:
Scanner scan = new Scanner("a b c");
String str1 = scan.next();
int val1 = map.get(str1);
Now val
has the value 1
stored in it, which you associated with the String
"a" earlier.
The next step is writing a parser for the arithmetic. That's a whole different animal to deal with. If you're looking for something quick to implement, I recommend trying to use Reverse Polish Notation if that's possible. In any case, I don't believe that there's a quick solution that will do all of this including the parsing automatically.
You might want to take a look at this link here: Evaluating a math expression given in string form. It's not quite what you're trying to do, but could be helpful.