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

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";   
share|improve this question
  • 2
    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
  • 2
    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
  • 1
    @CoreyFord In Java, you can always use an if instead of the ternary operator. – Turing85 Apr 13 at 18:46

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.376432 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO