How to Check if a Character Is a Number in Java

Haider Ali Feb 02, 2024
How to Check if a Character Is a Number in Java

There’s a simple built-in method that we can use to check if a character is a number in Java. Let’s dive in to know it.

Check if a Character Is a Number in Java

The public static boolean method is known as isDigit(). You only need to put the character variable inside the parenthesis and return a relevant boolean value. In the code example down below, we have explained everything in a very simple way.

import java.util.*;
public class Main {
  public static void main(String[] args) {
    // public static boolean isDigit(char ch)
    // Determines if the specified character is a digit.
    char c1 = '6';
    if (Character.isDigit(c1)) {
      System.out.println("Character Is Number");
    } else {
      System.out.println("Character is not a number");
    }
  }
}

Output:

Character Is Number
Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Java Character