Without any inbuilt method, we can check if a given character is alphanumeric or not. An alphanumeric character is a character that is either a number or an alphabet. The logic to find out if a character is alphanumeric is listed below:
'0'
and less than or equal to 9
?a
and less than or equal to z
?A
and less than or equal to Z
?We can see if a character is an alphanumeric character with these given conditions above.
if ((character >= '0' && character <= '9') || (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')){ // character is alphanumeric }else{ // character is not alphanumeric }
character
: This is the character we want to check if it is alphanumeric or not.
class HelloWorld { // create function to check if alphanumeric static void checkIfAlphanumeric(char character){ if ((character >= '0' & character <= '9') || (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')){ System.out.println(character +" is alphanumeric"); }else{ System.out.println(character +" is not alphanumeric"); } } public static void main( String args[] ) { // check if some characters are alphanumeric checkIfAlphanumeric('e'); checkIfAlphanumeric('1'); checkIfAlphanumeric('r'); checkIfAlphanumeric('%'); checkIfAlphanumeric(','); } }
RELATED TAGS
CONTRIBUTOR
View all Courses