Check if a String Contains Only Alphabets in Java using ASCII Values - GeeksforGeeks

10 Feb, 2022
Given a string, now as of we all know that the task is to check whether a string contains only alphabets. Now we will be iterating character by character and checking the corresponding ASCII value attached to it. If not found means there is some other character other than “a-z” or “A-Z”. If we traverse the whole string and land up on finding all characters in a string within this domain pool then the string is surely only alphabetic.
Illustrations: 
Input  : GeeksforGeeks
Output : True
 
Input  : Geeks4Geeks
Output : False
 
Input  : null
Output : False
In this article, the characters of the string are checked one by one using their ASCII values.
Algorithm: 
Get the stringMatch the string: Check if the string is empty or not. If empty, return falseCheck if the string is null or not. If null, return false.If the string is neither empty nor null, then check the string characters one by one for alphabet using ASCII values.Return true if matched
Get the string
Match the string: Check if the string is empty or not. If empty, return falseCheck if the string is null or not. If null, return false.If the string is neither empty nor null, then check the string characters one by one for alphabet using ASCII values.
Check if the string is empty or not. If empty, return false
Check if the string is null or not. If null, return false.
If the string is neither empty nor null, then check the string characters one by one for alphabet using ASCII values.
Return true if matched
Example 1:
Java
// Java Program to Check if a String Contains Only Alphabets// Using ASCII values // Importing required classesimport java.util.*; // Class 1// Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Creating object of helper class        Helper h = new Helper();         // Print statement        System.out.println(            h.isStringOnlyAlphabet("geeksforgeeks"));    }} // Class 2// Helper classclass Helper {     public static boolean isStringOnlyAlphabet(String str)    {         // If string is empty or null        if (str == null || str.equals("")) {             // Return false            return false;        }         // If we reach here we have character/s in string        for (int i = 0; i < str.length(); i++) {             // Getting character at indices            // using charAt() method            char ch = str.charAt(i);            if ((!(ch >= 'A' && ch <= 'Z'))                && (!(ch >= 'a' && ch <= 'z'))) {                return false;            }        }         // String is only alphabetic        return true;    }}
true
Example 2:
Java
// Java program to check if String contains only Alphabets// Using ASCII Values // Main classclass GFG {     // Method 1    // To check String for only Alphabets    public static boolean isStringOnlyAlphabet(String str)    {         // If there is no string        if (str == null || str.equals("")) {            return false;        }         // Iterating as now we encounter string        // using length() method        for (int i = 0; i < str.length(); i++) {             char ch = str.charAt(i);             // Checking for any other aphabetic character            // present in string            if ((!(ch >= 'A' && ch <= 'Z'))                && (!(ch >= 'a' && ch <= 'z'))) {                 // Then string is not only alphabetic                return false;            }        }         // If we reach here, it means        // string is only aphabetic        return true;    }     // Method 2    // Main method    public static void main(String[] args)    {         // Checking for True case        System.out.println("Test Case 1:");         String str1 = "GeeksforGeeks";        System.out.println("Input: " + str1);        System.out.println("Output: "                           + isStringOnlyAlphabet(str1));         // Checking for String with numeric characters        System.out.println("\nTest Case 2:");         String str2 = "Geeks4Geeks";        System.out.println("Input: " + str2);        System.out.println("Output: "                           + isStringOnlyAlphabet(str2));         // Checking for null String        System.out.println("\nTest Case 3:");         String str3 = null;        System.out.println("Input: " + str3);        System.out.println("Output: "                           + isStringOnlyAlphabet(str3));         // Checking for empty String        System.out.println("\nTest Case 4:");         String str4 = "";        System.out.println("Input: " + str4);        System.out.println("Output: "                           + isStringOnlyAlphabet(str4));    }}
Test Case 1:
Input: GeeksforGeeks
Output: true

Test Case 2:
Input: Geeks4Geeks
Output: false

Test Case 3:
Input: null
Output: false

Test Case 4:
Input: 
Output: false
Related Articles:
Check if a string contains only alphabets in Java using Lambda expression
Check if a string contains only alphabets in Java using Regex
solankimayank
sumitgumber28
simmytarika5
ASCII
Java-String-Programs
Java-Strings
Java
Java Programs
Strings
Java-Strings
Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
Singleton Class in Java
Set in Java
Multithreading in Java
Initializing a List in Java
Convert a String to Character Array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class