Java Program To Check Whether Mobile Number Is Valid Or Not

While developing applications, You need to check whether the user input is valid number or not. Lets take mobile Number as an example. You have to check whether mobile no. entered by the user contains valid or not. For those who have developed some applications, they may have come across this requirement.

Below, this program checks whether mobile number entered by user has 10 digits only and declares whether its Valid or not.

Java Code :-

class Utility
{
    static boolean numberOrNot(String input)
    {
        try
        {
            Integer.parseInt(input);
        }
        catch(NumberFormatException ex)
        {
            return false;
        }
        return true;
    }
}
public class CheckMobileNumber
{
    public static void main(String[] args)
    {
        System.out.println("Enter your mobile number");
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        if(Utility.numberOrNot(input) && (input.length() == 10))
        {
            System.out.println("Good!!! You have entered valid mobile number");
        }
        else
        {
            System.out.println("Sorry!!!! You have entered invalid mobile number. Try again...");
        }
        }
    }

Leave a Reply

avatar
  Subscribe  
Notify of