If statement using KeyCharaters
-
11-12-2019 - |
Question
public static void displayInfo(KeyEvent e){
int id = e.getID();
String keyString;
char c = e.getKeyChar();
keyString = ""+c;
if (keyString=="w"){
System.out.print("FACE");
}
}
this is my code and for whatever reason it wont work. Help please?
Solution
You should not compare strings with the ==
operator. Instead, use the equals
method. So, change this line:
if (keyString=="w"){
to this:
if (keyString.equals("w")){
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow