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

2018/1/30

Greenfoot.ask() causing number format exception (null)

temch55 temch55

2018/1/30

#
When trying to use Greenfoot.ask() in the world constructor, it works the first time. If you exit Greenfoot and then go back in, it no longer works. Instead, the line appears to just get skipped! It has nothing to do with the fact that I convert the string to an int... prompt for input just doesn't even appear if you try to run it again after exiting. public void populateWorld() { addObject(new Crab(), 300, 300); addObject(new Lobster(), 90, 70); addObject(new Lobster(), 390, 200); addObject(new Lobster(), 360, 500); String x = Greenfoot.ask("How many worms?"); //seems to only work the first time. int worms = Integer.parseInt(x); for (int i=0; i < worms; i++) addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); }
danpost danpost

2018/1/30

#
That is crazy, because for the last half hour or so I was working with just that -- using Greenfoot.ask in a constructor. I had not come across any issues with it; however, I have not yet closed out and opened it again. It may just be that without some world already having been created, it has no width to place the text in, thereby disallowing it. I say this because I did change my world to a (1, 1, 1) size and it did set the input box to the previous width size. I am glad you came across this because I was about to inform someone else that it would actually be possible, where this proves it is not feasible. Input seems impossible prior to the scenario being started unless strictly using swing components for both prompt and input.
You need to login to post a reply.