Check If Value is ASCII 7-bit Alphabetic in Java



In this article, we will discover how we can check whether a value entered is an ASCII 7-bit alphabetic character in Java. This process involves whether a character belongs to the ranges 'a' to 'z' or 'A' to 'Z' in the ASCII table. The character can be checked with some simple if statements to determine if it is in either of these two ranges and then corresponding messages are displayed.

Problem Statement

Given a character, write a Java program to check whether the entered value is an ASCII 7-bit alphabetic character or not.
Input
Initial character = 'r'

input = '1'
Output
Character: r

Given value is an alphabet!
Character: 1

Given value is not an alphabet!

Steps to check whether a character is an ASCII alphabetic character

The following are the steps to checkwhether a character is an ASCII alphabetic character -

  • Import the System class for output (already included by default in Java).
  • Initialize the character with a specific value.
  • Use a conditional if-else structure to check if the character falls between the ranges 'A' to 'Z' or 'a' to 'z'.
  • Display the appropriate message based on the result of the check.

Java Program to check if a character is an alphabet

The following is an example of checkingif a character is an alphabet -

public class Demo {
	public static void main(String []args) {
		char one = 'r';
		System.out.println("Character: "+one);
		if ((one >= 'A' && one <= 'Z') || (one >= 'a' && one <= 'z')) {
			System.out.println("Given value is an alphabet!");
		} else {
			System.out.println("Given value is not an alphabet!");
		}
	}
}

Output

Character: r
Given value is an alphabet!

Example with a non-alphabetic character

The following is an example with a non-alphabetic character -

public class Demo {
	public static void main(String []args) {
		char one = '1';
		System.out.println("Character: "+one);
		if ((one >= 'A' && one <= 'Z') || (one >= 'a' && one <= 'z')) {
			System.out.println("Given value is an alphabet!");
		} else {
			System.out.println("Given value is not an alphabet!");
		}
	}
}

Output

Character: 1
Given value is not an alphabet!

Code Explanation

In both examples, the program begins by initializing a character and then checks whether it falls within the ASCII ranges for upper-case ('A' to 'Z') or lower-case ('a' to 'z') letters. If the character is within these ranges, it is considered an alphabetic character, and the program prints that the "Given value is an alphabet!". Otherwise, the program prints "Given value is not an alphabet!" for non-alphabetic characters like digits or symbols.
Updated on: 2024-11-15T18:42:19+05:30

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements