-1

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";   
Raman Mishra
  • 2,635
  • 2
  • 15
  • 32
D74
  • 41
  • 5
  • 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 '18 at 18:41
  • 2
    one problem I see is that you try to compare strings with == instead of ```string.equals(otherstring)``` – luksch Apr 13 '18 at 18:41
  • For reference: [What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - [How do i compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Turing85 Apr 13 '18 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. https://stackoverflow.com/questions/3085681/java-operator – Corey Ford Apr 13 '18 at 18:43
  • The checking of the comparison works in both cases (In this instance, however I know using .equals is the better way)... However thats not solved my problem – D74 Apr 13 '18 at 18:45
  • 1
    @CoreyFord In Java, you can always use an `if` instead of the ternary operator. – Turing85 Apr 13 '18 at 18:46
  • @archiebaker the `==`-check on `String`s (or object in general) does work, but it does not do what most people think it does. – Turing85 Apr 13 '18 at 18:47
  • What exactly do you want to do? Do you want to select the value of the `Rotor_I` variable is `slowRotor` is `"Rotor_I"`? – lexicore Apr 13 '18 at 19:29
  • Is it `slow` or `Slow` or once this, once that? And maybe you're after a Map, where a name can dynamically map to some value. – user unknown Apr 13 '18 at 20:27

1 Answers1

0

I don't exactly know what you want and we could use a little more explanation, but I'm guessing your code would be something like:

if (rotor.equals("Slow")) {          
    resultAfterRotor = Character.toString((slowRotor.charAt(position))); 
} else if (rotor.equals("Medium")) {
    resultAfterRotor = // ... something else
} else if (rotor.equals("Fast")) {
    resultAfterRotor = // ... something else
}
// ...

Instead you could use a switch statement which avoids doing a comparison for each case as you would with if-else-if-else-if. (for few cases though (~3 maybe), switch will compile to if-else bytecode since I guess that's fastest):

private final static String slowRotor = "EKMFLGDQVZNTOWYHXUSPAIBRCJ";
private final static String mediumRotor = "...";
private final static String fastRotor = "...";

public static void main(String[] args){
    String rotor = GUI.getRotorInPosition("slow");
    int position = (alphabet.indexOf(result));
    String resultAfterRotor = getResultAfterRotor(rotor);
    // ...
}

private String getResultAfterRotor(String rotor) {
    switch(rotor){
        case "Slow": {
            return Character.toString(slowRotor.charAt(position)); 
        }
        case "Medium": {
            return Character.toString(mediumRotor.charAt(position)); 
        }
        case "Fast": {
            return Character.toString(fastRotor.charAt(position)); 
        }
    }
}

Or you could use a Map

private static final Map<String, String> rotorMap = new HashMap<>();
static {
    rotorMap.put("Slow", "EKMFLGDQVZNTOWYHXUSPAIBRCJ");
    rotorMap.put("Slow2", "...");
    rotorMap.put("Slow3", "...");
}

public static void main(String[] args){
    String slowRotor = GUI.getRotorInPosition("slow");
    int position = (alphabet.indexOf(result));
    String resultAfterRotor = Character.toString(rotorMap.get(slowRotor).charAt(position));
    // ...
}
xtratic
  • 4,600
  • 2
  • 14
  • 32