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

2016/3/5

Create as many actors as the user wants, How do I do that?

Dino.Chia Dino.Chia

2016/3/5

#
Well, first I want to explain that I'm from Colombia so my English isn't very good. Okay, it supossed i have to ask the user how many warriors does he wants in the world, I try to do it with an Greenfoot.ask() and then convert it to an int with Integer.parseInt(), but it keeps sending me the error : java.lang.NumberFormatException: null the code is this:
public void randomGuerreros(){
        int value = Integer.parseInt(Greenfoot.ask("Cuantos guerreros"));
        for(int i=0; i<value; i++) {
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            if (i%2==0){
                Guerrero guerrero = new Guerrero("azul");
                addObject(guerrero, x, y);
                guerrero.setTribu();
            }
            else{
                Guerrero guerrero = new Guerrero("rojo");
                addObject(guerrero, x, y);
                guerrero.setTribu();
            }
        }
    }
Super_Hippo Super_Hippo

2016/3/5

#
I just tested it out. For me, this code works as long as I only type in numbers. Actually I don't know how the 'ask' method can return 'null'. If I type in a letter, then it gives me this error too (with the String instead of null). (I created a new class and removed the parameters for the creation of them (azul+rojo) and the method called on them (setTribu) to test it out.)
You need to login to post a reply.