Home How would I use the result of a variable as a variable rather than plain text

# How would I use the result of a variable as a variable rather than plain text

user10137
1#
user10137 Published in September 19, 2018, 11:18 am

The problem I have is "slowRotor" returns the value "Rotor_I".

I would like to use the result of slowRotor (Rotor_I) as the variable Rotor_I and not the "plain text" so to speak.

I'm wondering if that is possible in Java and if so, how it would be done (As i'm trying to avoid lots of if statements)

    String slowRotor = GUI.getRotorInPosition("slow");
int position = (alphabet.indexOf(result));
String resultAfterRotor = "";
if (Rotor == "Slow") {
resultAfterRotor = Character.toString((slowRotor.charAt(position)));
}


And

static String alphabet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String Rotor_I = "EKMFLGDQVZNTOWYHXUSPAIBRCJ";

• There is no eval in Java. However, I suspect you have here an X/Y problem, so perhaps you can edit the question to provide more details on what you are trying to accomplish. – KevinO Apr 13 at 18:41
• one problem I see is that you try to compare strings with == instead of string.equals(otherstring) – luksch Apr 13 at 18:41
• For reference: What is the XY problem? - How do i compare strings in Java? – Turing85 Apr 13 at 18:43
• I don't think this can be done without an if statement. However the ? operator may be a "short" if statement that you could utilise. stackoverflow.com/questions/3085681/java-operator – Corey Ford Apr 13 at 18:43
• @CoreyFord In Java, you can always use an if instead of the ternary operator. – Turing85 Apr 13 at 18:46