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

2018/5/12

Password

CarmanRose CarmanRose

2018/5/12

#
I was creating a game and wanted to use a password system to allow the player to begin right back where they left off if they leave the game, with different passwords. However, I was having trouble trying to use the Greenfoot.ask method to do so, so I just wanted to ask how you would set it up.
danpost danpost

2018/5/12

#
CarmanRose wrote...
I was having trouble trying to use the Greenfoot.ask method to do so
What did you try? how was it troublesome?
CarmanRose CarmanRose

2018/5/13

#
I tried setting a string as a prompt that would be asked, and then another empty string that would take the users response, being input = Greenfoot.ask(prompt); Then taking the input and saying if(input == "password") to set to a world, but it just doesn't do anything
danpost danpost

2018/5/13

#
CarmanRose wrote...
if(input == "password")
Here is the problem. You are taking two String objects and asking if they are the same object. That is, you are not asking about what the two distinct String objects contain as far as their characters. You compare the contents of two strings using the equals method:
if ("password".equals(input))
It is always best to put the literal (or known entity) first (so you are not calling a method on a null object).
You need to login to post a reply.