If you want to allow only numbers, you should use a regular expression to match the whole string (add +
to check for at least one digit). Also, use matches()
method instead of find()
to match the whole input string (your problem is that find()
finds the first match and then doesn't try to match the rest):
pattern = Pattern.compile("[0-9]+");
matcher = pattern.matcher(stNumber);
if(matcher.matches())
else
error