Skip to main content

Command Palette

Search for a command to run...

Java Programming : Alphabet, digit or special character

Published
2 min readView as Markdown
Java Programming : Alphabet, digit or special character

Example

Input
0
Output
It is a digit

Required to Know

Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out.print(), and use of next().charAt(0), Use of If-else, and Use of Data types.

Steps

  • Take the character as input from the user.

  • We have to whether the conditions fall under this range or not.

    • a - z or A - Z

    • 0 - 9

  • If the condition falls within the range of a - z or A - Z, print it in an alphabet.

  • If the condition falls within the range of 0 - 9, print it as a digit. Otherwise, print it as a special character.

Program

import java.util.Scanner;

public class alphabet {

  /**
   * This program checks if a character is an alphabet, a digit, or a special character.
   *
   */
  public static void main(String[] args) {
    // Create a Scanner object to read user input.
    Scanner sc = new Scanner(System.in);

    // Prompt the user to enter a character.
    System.out.print("Enter the character: ");
    char ch = sc.next().charAt(0);

    // Check if the character is an alphabet.
    if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
      // The character is an alphabet.
      System.out.println("It is an alphabet");
    } else if (ch >= '0' && ch <= '9') {
      // The character is a digit.
      System.out.println("It is a digit");
    } else {
      // The character is a special character.
      System.out.println("It is a special character");
    }
  }
}

More from this blog

TheCodingcoder

60 posts

Empowering the coding community by simplifying complex tasks.