Sample Programs

Sample java program to check if the given String contains only alphabets

 
 public class Utility {

    public static void main(String[] args) {

        System.out.println("Given string contains only alphabets  = " + isAlpha("OnlyAlphabets"));
		
        System.out.println("Given string contains only alphabets  = " + isAlpha("Only Alphabets 1"));
    }
	
    /**
     * Return whether the specified string contains only alphabets.
     * @param String s
     * @return true if the string contains only alphabets, false if it has numbers too.
     */
    public static boolean isAlpha(String s) {
        s = s.toUpperCase();

        for (int i = 0; i < s.length(); i++) {
            int c = (int) s.charAt(i);

            if (c < 65 || c > 90)
                return false;
        }

        return true;

    }
 }




blog comments powered by Disqus