Problem with Strings and switch case

Hi, i have problem pasing string in switch case, say i under java 1.5 and need 1.7 or 7 but in proyect properties say im using 1.7 .

@freebook21 said: Hi, i have problem pasing string in switch case, say i under java 1.5 and need 1.7 or 7 but in proyect properties say im using 1.7 .

First of all, you have to make sure source compatibility is set to 1.7. I’m not sure which setting exactly you are referring to above.

Second, you might consider using enums instead. There are some decent design reasons why it took Java so long to support switch/case on strings. Not necessarily true in all cases but as a general rule you are better off with enums.

Yeah nvm, i update jdk to but didnt work, I just wanted to use Strings for better identification for loading models.

@freebook21 said: Yeah nvm, i update jdk to but didnt work, I just wanted to use Strings for better identification for loading models.

Where are you setting the 1.7 thing?

How are you attempting to use the switch/case? Maybe we can provide some design advice if we can see your use-case.

This part of the code:

public class Enemigo extends SimpleApplication implements ActionListener, AnalogListener,PhysicsCollisionListener{
Node nodo_Enemigo;
public Spatial A1;
public Spatial A2;
public Spatial gm;
Node nodetA;
Node instGM = new Node();
public GhostControl ghostControltA;
public Spatial GeomB;
public BulletAppState bulletAppState;
public float eneX = 5.0f;
public float eneY = 5.0f;
public float eneZ = 5.0f;
public float posX_Enemigo, posY_Enemigo, posZ_Enemigo;
public String num_Enemigo;

public Enemigo( String numbreEnemigo)
{
this.num_Enemigo = numbreEnemigo ;
// this.posX_Enemigo = rec_X;
//this.posY_Enemigo = rec_Y;
// this.posZ_Enemigo = rec_Z;

}

public void Elegir(){

String nuevoEnemigo = num_Enemigo;
switch (nuevoEnemigo)
        {
            case 0100: 
                    this.posZ_Enemigo = 90;
            break;
        
        
        }

}

and the warning underline “switch (nuevoEnemigo)” and over it apear a windows with : String in switch are not suported in -source 1.5 (use -source 7 or higher to enable string in switch)

But switch work normally out of String.

This looks as it can be perfectly made with an enum, is there any reason you decided against them?

So, yeah, an enum would be a lot better here. Define a
public enum MyEnumNameWhatever {
0100, 0200, Whatever
}

Or make it EnemyType and given them logical names for the different types instead of raw IDs.

Then use that instead of string. As it is, your switches will be relying on a lot of fall-through default behavior. So on the other hand, if you find yourself using only a couple ‘case’ statements out of all of the possibilities then if statements are actually better from a readability standpoint.

Regarding the error, though, it is saying that your compile settings are still set to 1.5. Open the project properties (right click on the project in the project tree). Then go to “Sources” and change the Source/Binary drop down at the bottom to 1.7.