Sample Programs

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

 
 public class Utility {

    public static void main(String[] args) {

        System.out.println("Given string contains only numbers  = " + isNumeric("Alphabets"));
		
        System.out.println("Given string contains only numbers  = " + isNumeric("12456"));
    }
	
    /**
     * Return whether the specified string contains only numbers.
     * @param String s
     * @return true if the string contains only numbers, false if it has alpabets/special chars too.
     */
    public static boolean isNumeric(String s) {
        for (int i = 0; i < s.length(); i++) {
            int c = (int) s.charAt(i);
            if (c < 48 || c > 57)
                return false;
        }

        return true;

    }
 }




blog comments powered by Disqus