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

2013/8/7

Using names like Strings

Kartoffelbrot Kartoffelbrot

2013/8/7

#
Can I use names like Strings? It would be easier like this:
//the levels are JButtons
        for(int i = levelMAX; i > levelUnlocked; i--){
            String object = "level"+i;
            object.setEnabled(false);
        }
I know, this way it won't work. But maybe there is a way. Otherwise I will use an array.
Kartoffelbrot Kartoffelbrot

2013/8/7

#
Ok I made it with an array now, that's easier than using thousand if statements, too, but it would still be interesting to know.
Zamoht Zamoht

2013/8/7

#
The way you wrote it should work.
int i = 10;
String something = "Level " + 1;
System.out.print(something);
This will print "Level 1".
Kartoffelbrot Kartoffelbrot

2013/8/7

#
I know. But I have an object called level1. With "level"+i I want to have all my objects from level1 to level10 etc. to use this objects.
Gevater_Tod4711 Gevater_Tod4711

2013/8/7

#
I'm not shure if this works because I didn't test but you could try to use java.lang.Class. Maybe the method forName works. You'll have to try.
Kartoffelbrot Kartoffelbrot

2013/8/7

#
Thank you both.
Zamoht Zamoht

2013/8/7

#
Oh sorry got confused by "I know, this way it won't work." Well I hope that Gevater_Tod4711's answer helped then.
You need to login to post a reply.