This site requires JavaScript, please enable it in your browser!
Greenfoot back
kOmaster476
kOmaster476 wrote ...

2014/12/22

Changing Java Source in Greenfoot projects

kOmaster476 kOmaster476

2014/12/22

#
I am trying to use a switch statement in my Greenfoot project but I get the following error message "Strings in switch are not supported in -source 1.6 (use -source 7 or higher to enable Strings in switch)". Can someone help me change my java source from 1.6 to 7. NOTE: I have JDK 1.8.0_25, the latest on Oracle.com/Java . Thanks for your help!
erdelf erdelf

2014/12/22

#
As far as I know it's a greenfoot limitation, you cant really change it
kOmaster476 kOmaster476

2014/12/24

#
Oh, well that sucks... Thanks "erdelf"!
danpost danpost

2014/12/24

#
What exactly are you trying to do with using Strings in a switch statement? Maybe if you list the String values that you want the switch to segregate here, we can help you figure out a way around the limitation.
kOmaster476 kOmaster476

2014/12/28

#
I recently learned how to use switch in Java and knowing that Greenfoot is a Java based environment, I figured that using a switch statement would be better than using a bunch of "if" statements. I just want to use the switch for convenience and to get more familiar with the structure. Its not critical for me to use switch but I just want to get some practice with it.
danpost danpost

2014/12/28

#
I was thinking along the following lines when dealing with Strings in a switch block. Let us say you were doing a chess program and you had the pieces hold their types in Strings. Then, instead of:
1
2
if ("Knight".equals(piece.getType())) ...
else if ("Pawn".equals(piece.getType())) ... // etc.
you could do this:
1
2
3
4
5
switch ("Pawn  KnightBishopRook  Queen King  ".indexOf(piece.getType())/6)
{
    case 0: ...; break; // pawn
    case 1: ...; break; // knight
    case 2: ...; break; // bishop   etc.
You need to login to post a reply.