If I have the strings “hello8459” and “1234”, how would I go about detecting which one had the alphabetical characters in? I’ve been trying:
//Checking for numerics in an if...
Pattern.matches("0-9", string1);
However it doesn’t work at all. Can anyone advise?
“0-9” matches just that, the string: “0-9”.
What you probably meant to do is “[0-9]+” which matches one ore more digits.
And you can use String’s matches(…) method:
Be careful though, when parsing to a primitive int or long after checking ‘onlyDigits’: it might be a large number like 123145465124657897456421345487454 which does not fit in a primitive data type but passes the matches(…) test!