📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In many scenarios, you may need to validate a string to ensure it contains only letters and/or digits. Java provides various methods to perform this check efficiently. In this guide, we will create a Java program that checks whether a given string consists solely of letters and digits.
Problem Statement
Create a Java program that:
- Takes a string as input.
- Check if the string contains only letters (A-Z, a-z) and digits (0-9).
- Displays the result indicating whether the string is valid.
Example 1:
- Input:
"Java123"
- Output:
The string "Java123" contains only letters and digits.
Example 2:
- Input:
"Java@123"
- Output:
The string "Java@123" contains characters other than letters and digits.
Solution Steps
- Prompt for Input: Use the
Scanner
class to read a string input from the user. - Check the String: Use the
String
class methods to verify if the string contains only letters and digits. - Display the Result: Print whether the string is valid (contains only letters and digits) or not.
Java Program
import java.util.Scanner; /** * Java Program to Check If the String Contains Only Letters or Digits * Author: https://www.javaguides.net/ */ public class CheckLettersDigits { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Prompt the user for input System.out.print("Enter a string to check if it contains only letters and digits: "); String input = scanner.nextLine(); // Step 2: Check if the string contains only letters and digits boolean isValid = containsOnlyLettersAndDigits(input); // Step 3: Display the result if (isValid) { System.out.println("The string \"" + input + "\" contains only letters and digits."); } else { System.out.println("The string \"" + input + "\" contains characters other than letters and digits."); } } // Method to check if a string contains only letters and digits public static boolean containsOnlyLettersAndDigits(String str) { return str.chars().allMatch(Character::isLetterOrDigit); } }
Explanation
Step 1: Prompt for Input
- The program uses the
Scanner
class to read a string input from the user. This string will be checked to see if it contains only letters and digits.
Step 2: Check If the String Contains Only Letters and Digits
- The
containsOnlyLettersAndDigits
method is called with the input string as the argument. - This method uses
str.chars().allMatch(Character::isLetterOrDigit)
to verify that all characters in the string are either letters or digits:str.chars()
converts the string into anIntStream
of character codes.allMatch(Character::isLetterOrDigit)
checks if every character in the stream is a letter or digit.
Step 3: Display the Result
- The program prints a message based on whether the string contains only letters and digits:
- If the string is valid, it prints that the string contains only letters and digits.
- If the string contains other characters, it prints that the string contains characters other than letters and digits.
Output Examples
Example 1:
Enter a string to check if it contains only letters and digits: Java123 The string "Java123" contains only letters and digits.
Example 2:
Enter a string to check if it contains only letters and digits: Java@123 The string "Java@123" contains characters other than letters and digits.
Conclusion
This Java program efficiently checks whether a given string contains only letters and digits. By using the Stream API
and the Character.isLetterOrDigit
method, the program performs the validation in a concise and readable manner. This method is useful in various applications where input validation is required, such as user authentication, data processing, and more.
Comments
Post a Comment
Leave Comment