- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find If a given String contains only letters in Java?
To verify whether a given String contains only characters −
- Read the String.
- Convert all the characters in the given String to lower case using the toLower() method.
- Convert it into a character array using the toCharArray() method of the String class.
- Find whether every character in the array is in between a and z, if not, return false.
Example
Following Java program accepts a String from the user and displays whether it is valid or not.
import java.util.Scanner; public class StringValidation{ public boolean validtaeString(String str) { str = str.toLowerCase(); char[] charArray = str.toCharArray(); for (int i = 0; i < charArray.length; i++) { char ch = charArray[i]; if (!(ch >= 'a' && ch <= 'z')) { return false; } } return true; } public static void main(String args[]) { Scanner sc= new Scanner(System.in); System.out.println("Enter a string value: "); String str = sc.next(); StringValidation obj = new StringValidation(); boolean bool = obj.validtaeString(str); if(!bool) { System.out.println("Given String is invalid"); }else{ System.out.println("Given String is valid"); } } }
Output
Enter a string value: 24ABCjhon Given String is invalid
- Related Articles
- Check if the String contains only unicode letters in Java
- How to check if a string contains only whitespace letters in Python?
- How to check if a string contains only lower case letters in Python?
- How to check if a string contains only upper case letters in Python?
- Check if the String contains only unicode letters and space in Java
- Check if the String contains only unicode letters or digits in Java
- Check if the String contains only unicode letters, digits or space in Java
- Java Program to validate if a String contains only numbers
- Check if a string contains only alphabets in Java using Regex
- How to check if a Python string contains only digits?
- How to check if a string contains only decimal characters?
- Is it possible to check if a String only contains ASCII in java?
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- How to check if a string only contains certain characters in Python?

Advertisements