0

I'm making a game, and i want the controls to be editable. well, i've got that part down, but they are being read and changed in a .txt file. that is the way i wanted it to work for now. the values are stored as the key value (ie. KeyEvent.VK_W is equal to 83, so the value for the line is 83). I also have it reading the values and saving them to a String array variable in my core class. In my key event class, the one that handles the pushing of the keys, i have it refering to the array to check if a command key was pushed. i'm continuously getting this error: case expressions must be constant expressions when i try it. here is the WRONG code:

switch(key){
  case Integer.parseInt(commands[1]):
      ...
      break;
}

and i get that error. the value of commands[1] is 83. it is the value for "W". here is my declaration of the variable:

for (int i = 0; i < commands.length; i++) {
                commands[i] = io.readSpecificLine(FILES.controlsFileFinalDir,
                        i + 1);
}

and if i have it print out every value, it does work. i've tried making the array final but that didnt work. i've run across the solution before, about 2 years ago, but i cant find it again. does anyone have any ideas on how to fix this? thanks in advance!

0

2 Answers 2

4

As the compiler says, the case expressions must be constant expressions. You can't use an array element as a case expression. You can simply use an if/else if/else clause instead.

Sign up to request clarification or add additional context in comments.

4 Comments

i feel like the switch is so much easier and neater though. i was really looking for a way to fix the error, while keeping (hopefully) the array and for sure the switch :\
See this link for a description of what counts as a compile-time constant.
@mamdouh: You can use strings as switch labels in java 7, but they must still be constants. See this link
i finally just went to the stupid if/else statements. i accepted it because thats the solution i used. thanks!
1

You can't use non-constant expressions in case statements. An alternative approach is to build a map from values to the actions. So instead of this (which doesn't actually make any sense to me):

switch (key) {
case Integer.parseInt(commands[1]):
    // action 1
    break;
// other cases...
default: 
    // default action
}

You can do something like this:

static Map<Integer, Runnable> keyMap = new HashMap<Integer, Runnable>();
static {
    keyMap.put(83, new Runnable() {
        @Override
        public void run() {
            // actions for code 83
        }
    });
    . . .
}

(If it makes more sense, this could also be done on a per-instance basis instead of as a static map.) Then later:

Runnable action = keyMap.get(Integer.parseInt(commands[1]));
if (action != null) {
    action.run();
} else {
    // default action
}

If you need to pass variables to your actions, you can define your own interface instead of using Runnable for the actions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.