0

Why does

KeyEvent.getKeyText(0).substring(0, 7) == "Unknown"

return false when

System.out.print(KeyEvent.getKeyText(0).substring(0, 7));

prints exactly "Unknown"?

radrow
  • 6,419
  • 4
  • 26
  • 53

1 Answers1

2

In Java Strings are objects, so you should not compare with ==. You have to call equals on strings to compare there content. If you compare them with == you compare them by reference.

KeyEvent.getKeyText(0).substring(0, 7).equals("Unknown");

An alternative would be contains:

KeyEvent.getKeyText(0).contains("Unknown");

See this post: Java String.equals versus ==

Community
  • 1
  • 1
morpheus05
  • 4,772
  • 2
  • 32
  • 47