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)