Write a program to validate the given mobile number.
Input String(Mobile Number) |
Output |
9912011220 |
true |
8801123480 |
true |
7123456094 |
true |
0128932655 |
true |
99480152463 |
false |
884579652314 |
false |
45210sd233*V7 |
false |
"" (EMPTY STRING) |
false |
null |
false |
class ValidateMobileNUmber
{
public static void main(String s[])
{
String mobileNumber = "9912834567";
boolean result = isValidMobileNumber(mobileNumber);
System.out.println("The given mobile number 9912834567 is valid : "+result);
}
public static boolean isValidMobileNumber(String mobileNumber)
{
boolean result = false;
return result;
}
}
Topic:
Conclusion Of Strings In Java
If you need explanation Read this topic
If you need Answer Take test on this topic
User comments below. All of them might not be correct.
we have to validate a given mobile number.... for validation conditions are - length shud be 10 ...also only numeric digits shud be there.... else not a mobile number..... firstly chek wthr string is not empty or null.. if yes thn return false... otherwise chek for following..... -->> 1) length checking -if mobilenumber.length()!=10 return false otherwize now chek for numeric digits..... we have to traverse each charactr... take a loop c=0 to mobilenumber.length() if ((int)mobilenumber.charAt(c) is between 48 to 58) den return true else false... here we are comparing with ascii values of 0-9 to chek digits... exit
Posted by Asma Mujtaba Khan 2015-01-07 06:48:55
first we need to check it whether it is a Number (Long) or Not So, Use Exception Handling and then if the String is a number then Check whether it has 10 digits or Not . If it has 10 digits then set result as true .. Not then Leave It ... No need to assign any value
Posted by ?????????? ????? 2015-01-07 06:49:39
To validate the mobile number certain Conditions should be satisfied:
1)It must have 10 digits.
2)It should not contain anything other than digits.
3)It should not be empty or null
To Validate given mobile number "IF ELSE" ladder is used below to check for various conditions:
--1st we have to check that mobileNumber is ot empty using length function.
-->if(mobileNumber=="" || mobileNumber.length()==0)
result=false;
--Then we have check that it should contain 10 digits for that we have to check each and every character of mobileNumber is digit.
We have to use the isdigit() method of Character class to check each character of mobileno is digit.
-->else if(mobileNumber.length()==10)
{
for(int i=0;i<mobileNumber.length();i++)
{
if( !Character.isDigit(mobileNumber.charAt(i)) )
{
result=false;
break;
}
else
{
result=true;
}
}
}
Posted by Mânïshå Mùlchåndânï 2015-01-07 07:19:50
ules:
* Must contain only 10 digits
* Must contain digits only...
Coding:
*if the String is null || empty than return false (mobileNumber==null)||(mobileNumber.length==0)
*num pass this String to try
Integer.parseInt(mobileNumber)
than if it contains other than digits than it will throw Exception so return false in the catch() Block..
try{Integer.parseInt(mobileNumber)}catch(Exception e){return false;}
* now check the length if it is !=10 than return false;if(mobileNumber.length()!=0) return false
*last return true
Posted by Uday Kumar 2015-01-07 16:29:39
This dose is now closed and the winners are Asma Mujtaba Khan, for 'First Correct Comment', ?????????? ?????, Uday Kumar, for 'Second Correct Comment'. The 'lucky liker' is Asad Ahmed. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.
Posted by Merit Campus 2015-01-31 04:28:14