We can check if a character is a digit in Java without using any built-in method. A digit character is any numeric character from 0
and 9
. The way to achieve this is by using an if-else
condition.
if(character <= '0' && character >= '9'){// character is digit}else{// character is not digit}
character
: This is the character that we're checking to see if it is a digit or not.
class HelloWorld {// create function to check if digtstatic void checkIfDigit(char character){if (character >= '0' & character <= '9'){System.out.println(character +" is digit");}else{System.out.println(character +" is not digit");}}public static void main( String args[] ) {// check if some characters are digitscheckIfDigit('e');checkIfDigit('1');checkIfDigit('3');checkIfDigit('%');checkIfDigit('4');}}
In the code above: