How to Count Only Letters in a String in Java

This is the simple but logic building java exercise for beginners. The program just counts letter in a given string e.g. a1B2C456 has only 3 Letters i.e aBC 12456 are not letters.

Source Code 


package packaga;
public class countLeters {
/**
     * @param args the command line arguments
     */
public static void main(String[] args) {
String str = "A1B2C3";
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i)))
count++;
}
System.out.println("Striing "+str+" has "+count + " letters.");
}
}

Output of the Program


Count Only Letters in a String in Java

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment