Check whether the entered value is a digit or not in Java


To check whether the entered value is a digit or not in Java, use the Character.isDigit() method.

We have a character to be checked.

char val = '5';

Now let us use the Character.isDigit() method.

if (Character.isDigit(val)) {
   System.out.println("Character is a digit!");
} else {
   System.out.println("Character is not a digit!");
}

Let us see the complete example now to check for Uppercase in Java.

Example

 Live Demo

public class Demo {
   public static void main(String []args) {
      System.out.println("Checking for digit...");
      char val = '5';
      System.out.println("Value: "+val);
      if (Character.isDigit(val)) {
         System.out.println("Character is a digit!");
      }else {
         System.out.println("Character is not a digit!");
      }
   }
}

Output

Checking for digit...
Value: 5
Character is a digit!

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements