This code will show how to use NumberForamtException to validate whether given string contains a valid integer or not.

parseInt() method of the Integer object uses to convert a given string to the integer. This method throws a NubmerPointerException if the given string is not a valid integer.

 public class NmbrVldExp {

    public static boolean validateNumber(String num) {
        try {
            return true;
        } catch (Exception e) {
            return false;

    public static void main(String[] args) {
        String num="6";

            System.out.println("Correct Number");
            System.out.println("Incorrect Number");