Sample Programs

Sample java program to check if the given String is alpha numeric

 
 import java.util.regex.Pattern;

 public class Utility {

    private static final Pattern VALID_ALPHA_NUM = Pattern.compile("[0-9a-zA-Z]*"); 
	
    public static void main(String[] args) {

        System.out.println("Given string contains alphabets  = " + isAlphaNumeric("Alphabets"));
		
        System.out.println("Given string contains numbers  = " + isAlphaNumeric("12456"));
		
        System.out.println("Given string is alphaNumeric  = " + isAlphaNumeric("Alpha12456"));
		
        System.out.println("Given string is alphaNumeric  = " + isAlphaNumeric("Alpha 12456"));
    }
	
    /**
     * Return whether the specified string is alpha numeric.
     * @param String s
     * @return true if the string is alpha numeric, false if it has special chars too.
     */
    public static boolean isAlphaNumeric(String s) {
        return VALID_ALPHA_NUM.matcher(s).matches();
    }
 }




blog comments powered by Disqus