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

2024/4/1

Greenfoot.ask() question

Cbazz Cbazz

2024/4/1

#
I'm using the Greenfoot.ask() method to set my static string variable 'lastKey' to whatever the last key pressed was. This works, however, when I try to use this with an If() statement ( if ( lastKey == "w" ) ) it doesn't work. Any reason?
Cbazz Cbazz

2024/4/1

#
(if the lastKey var isn't a part of my bindings array it will re-ask the question)
danpost danpost

2024/4/2

#
Cbazz wrote...
when I try to use this with an If() statement ( if ( lastKey == "w" ) ) it doesn't work. Any reason?
Yes. The literal is not the SAME String instance as what the variable lastKey may hold. It is a different instance containing the very same character string. Use the following for comparing the contents of the String objects:
if ("w".equals(lastKey))
Note that the variable name and the literal string may be swapped around; however, if the contents of the variable is null, an error will ensue (NullPointerException).
You need to login to post a reply.