🎓 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 (178K+ 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
Scannerclass to read a string input from the user. - Check the String: Use the
Stringclass 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
Scannerclass 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
containsOnlyLettersAndDigitsmethod 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 anIntStreamof 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment