I just want to print the phone number in the form of (123)456-55778 for my result to be valid. I have tried a couple of thing but I am not arriving to the answer. Please help.
import java.util.*;
public class TokenizingTelephoneNumbers {
/**
* @param args the command line arguments
*/
public static boolean validatePhone(String phone)
{
return phone.matches("([1-9]\\d{2}) [1-9]\\d{2}-\\d{4}");
//("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}"); book example
}
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.println("Please enter phone:");
String phone = input.nextLine();
System.out.println("\nResults:");
if(!validatePhone(phone))
{
System.out.println("Invlid phone number");
}
else
System.out.println(" Your phone number is correct.\n"
+ " Thank you");
}
}