I am trying to make a simple method which test to see if a provide String contains only numbers, to do this I am trying to use try and catch (just learnt it and I would like to practise putting it to use) where I try to parseInt() the given String and if there's an error (not a number) then it will catch it and return false;
public boolean checkNumber(String s){
try(Integer.parseInt(s)){
return true;
}
catch(Exception E){
return false;
}
}
It says I have a misplaced constructor.
Integer.parseIntExceptionwill be thrown and caught, which are generally a big order of magnitude slower than simple checks. Exceptions are for exceptional situations.