Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using arrays in switch case java

Tags:

java

I have code where what a switch statement is testing for depends on an array variable:

String shuff = Import.shuffle();
String[] form = new String[95];
for(int i = 0; i < 95; i++)
{
    form[i] = Format.shuffle(shuff, i);
}
switch(str)
{
case "a":
    x = 6;
    break;
case "b":
    x = 16; 
    break;
case "c":
    x = 23;
    break;
//So on and so forth
}

What I want to do is take the array form[] and use it as the case:

String shuff = Import.shuffle();
String[] form = new String[95];
for(int i = 0; i < 95; i++)
{
    form[i] = Format.shuffle(shuff, i);
}
switch(str)
{
case form[0]:
    x = 6;
    break;
case form[1]:
    x = 16; 
    break;
case form[2]:
    x = 23;
    break;
//So on and so forth
}

But when I try this, it gives the error "case expressions must be constant expressions". Two options to solve this come to mind, but I don't know how to do either. 1. use the array in the switch case somehow 2. use some sort of method that would look like this...

String shuff = Import.shuffle();
String[] form = new String[95];
for(int i = 0; i < 95; i++)
{
    form[i] = Format.shuffle(shuff, i);
}
switch(str)
{
case form[0].toString():
    x = 6;
    break;
case form[1].toString():
    x = 16; 
    break;
case form[2].toString():
    x = 23;
    break;
//So on and so forth
}

Is there any way to do either?

The Import.shuffle method takes a text file with 95 lines (each line being one character) and strings it together and Format.shuffle puts each of the original lines into individual array variables.

I can't convert this into an if else if chain because it's a 95 case switch (edit)

like image 736
Kai Arakawa Avatar asked Dec 21 '14 00:12

Kai Arakawa


2 Answers

You could find the index of str within form, then switch based on the index. For example:

String shuff = Import.shuffle();
String[] form = new String[95];
for(int i = 0; i < 95; i++)
{
    form[i] = Format.shuffle(shuff, i);
}
int index=Arrays.asList(form).indexOf(str);
switch(index)
{
case 0:
    x = 6;
    break;
case 1:
    x = 16; 
    break;
case 2:
    x = 23;
    break;
//So on and so forth
}

However, neither of your proposed solutions will work because, when the code is compiled, the compiler needs to know exactly what each case value is instead of just knowing that it is in a variable. I'm not sure the exact reasons, but it may be for optimization or for making sure cases are not duplicated (which will compile incorrectly, IIRC). The bottom line is that form[0] is not a constant and the compiler wants a constant.

like image 94
James Westman Avatar answered Sep 19 '22 12:09

James Westman


You can't. Java's switch statement requires that case labels be constant expressions. If your code does not work with that restriction, you'll need to use if...elseif...else constructions instead.

See §14.11 of the JLS:

SwitchLabel:
   caseConstantExpression:
   caseEnumConstantName:
   default :

like image 33
Alexis King Avatar answered Sep 23 '22 12:09

Alexis King