Given a string, the task is to write a Java program to check whether the string contains only alphabets or not. If so, then print true, otherwise false.
Explanation: The given string contains only alphabets so the output is true.
Explanation: The given string contains alphabets and numbers so the output is false.
Explanation: The given string is empty so the output is false.
Method 4: This approach uses the methods of Character class in Java
- The idea is to iterate over each character of the string and check whether the specified character is a letter or not using Character.isLetter() method.
- If the character is a letter then continue, else return false.
- If we are able to iterate over the whole string, then return true.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.