Write a program to input any character and check whether it is alphabet, digit or special character


The program is a Java program that takes a single character input from the user and checks whether it is an alphabet, number, or a special character.

  • The program starts by importing the java.util.Scanner package which allows user input to be taken from the console.
  • In the main method, the program prompts the user to enter a single character using the System.out.print method and reads the input using the Scanner object's next() method.
  • The program then checks whether the entered character is an alphabet or not using an if statement. The condition of the if statement checks if the entered character is between 'a' and 'z' or 'A' and 'Z' using the logical AND (&&) operator. If the condition is true, the program prints "This is an Alphabet" using the System.out.println() method.
  • If the entered character is not an alphabet, the program then checks if it is a number using an else if statement. The condition of the else if statement checks if the entered character is between '0' and '9'. If the condition is true, the program prints "This is a Number" using the System.out.println() method.
  • If the entered character is not an alphabet or a number, the program prints "This is a Special Character" using the System.out.println() method.
  • Finally, the program exits the main method, and the program execution ends.

Source Code

import java.util.Scanner;
class Alpha_Num_Spl
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the Values :");
		char ch = input.next().charAt(0);			
		if(ch>='a' && ch<='z' || ch>='A' && ch<='Z')
		{
			System.out.println("This is a Alphabet");
		}
		else if(ch>='0' && ch<='9')
		{
			System.out.println("This is a Number");
		}
		else
		{
			System.out.println("This is a Special Character");
		}
	}
}

Output

Enter the Values :34
This is a Number

Enter the Values :d
This is a Alphabet

Enter the Values :%
This is a Special Character

Example Programs