Want to find If a given String contains only letters in Java? Use the following Java program to find If a given String contains only letters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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."); } } } |
Program Output
1 2 3 |
Enter a string value: 24FreeWebMentor23980 Given String is invalid. |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.