Welcome to the Ranch
if () ... else throw new Exception(); ... catch() ...???
Never use Exceptions inside a method like that. All you achieve is making an if-else by a very inefficient route.
You have already been given suggestions how you might confirm the structure of your text.
What you are doing is parsing some text into an
int, and comparing the text with that
int (now boxed into an Integer) for equality. Since Strings and Integers are different classes, there is no way the equals() method will let that pass, so it instantly returns
false. If it doesn't parse to an
int, you suffer an exception.
What you can get from the
KeyEvent object is the actual key which was pressed. And even the
char which that key types. Now you can go to the
Character class, which has methods telling you whether that
char is a number, letter, space, etc etc.
Beware. I think it very likely there is somebody called
Anne-Marie O’Neill living within 2 miles of here. I definitely know people called Anne-Marie and O’Neill. so you are going to have to accept the - and the ’ characters, too.
You might do better to put your code into the keyTyped() method. Also be sure to use the @Override annotation on all methods in XYZAdapter objects.