Is digit java – Java Program to Check Character is Alphabet or Digit or Character

Is digit java: In the previous article we have discussed about Java Program to Check Character is Vowel or Consonant

In this article we are going to see how to check the input character is alphabet or digit or any special character using Java programming language.

Java Program to Check Character is Alphabet or Digit or Character

We have 255 characters and it consists of alphabets, digits, and special characters. So in this program we are going to check whether the input character is an alphabet or digit or any special character using different methods.

Method-1: Java Program to Check Character is Alphabet or Digit or Character By Using If Else Statement

Approach:

  • Declare a character variable as ch.
  • Prompt the user to Enter a character.
  • As we know every character has certain range of ASCII values as follows:
  1. A-Z = 65-90
  2. a-z = 97-122
  3. 0-9 = 48-57
  4. Rest are special characters
  • We are going to use nested if case. In first condition we are checking whether the input is alphabet or not, if not then check the second condition whether the input is digit or not if both conditions are wrong then the input character must be a special character.
  • Print the result.

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //create object of scanner class
        Scanner inp= new Scanner(System.in);
        System.out.print("Enter the Character: ");
        //taking input from user
        char ch=inp.next().charAt(0);
        //checking condition for alphabet
        if((ch>=65 && ch<=90) || (ch>=97 && ch<=122))
        {
            System.out.println(ch+" is an Alphabet.");
        }
        //checking condition for digit
        else if(ch>=48 && ch<=57)
        {
            System.out.println(ch+" is a digit.");
        }
        else
        {
            System.out.println(ch+" is a special character.");
        }
    }
}
Output:

Enter the Character: 4
4 is a digit.

Method-2: Java Program to Check Character is Alphabet or Digit or Character By Using Inbuilt Functions

Approach:

  • Take a character variable as ch.
  • Prompt the user to Enter a character.
  • There are some predefined functions available by which we can find whether the input character is alphabet or digit as follows:
  1. isAlphabetic(input-character) = checking whether the input is alphabet.
  2. isDigit(input-character) = checking whether the input is digit.
  • We are going to use nested if case. In first condition we are checking whether the input is alphabet or not, if not then check the second condition whether the input is digit or not if both conditions are wrong then the input character must be a special character.
  • Print the result.

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //create object of scanner class
        Scanner inp= new Scanner(System.in);
        System.out.print("Enter the Character: ");
        //taking input from user
        char ch=inp.next().charAt(0);
        //checking condition for alphabet
        if(Character.isAlphabetic(ch))
        {
            System.out.println(ch+" is an Alphabet.");
        }
        //checking condition for digit
        else if(Character.isDigit(ch))
        {
            System.out.println(ch+" is a digit.");
        }
        else
        {
            System.out.println(ch+" is a special character.");
        }
    }
}
Output:

Enter the Character: ]
] is a special character.

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: