I am learning Java for the first time, and a homework question asks that we create an address list, where invalid characters are caught in our phone number formats. Can I do this through a 'set' method? My code is as following:
public void sethomePhone(String value) {
String regex = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]
{4})$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
if (matcher.matches() == true){
homePhone = value;
} else {
System.out.println(value + "is not a valid phone number.");
}
This doesn't seem to be working though, since when I test it in the main class even invalid phone numbers are getting passed through as valid. Do I need to create a separate method for this?
Thanks, and sorry if this is a stupid question; I'm only a month in!
Thanks all for answering. I modified the code trying both suggested regex formats, and it's still allowing invalid numbers through. I tried:
public void sethomePhone(String value) {
String regex = "^(([0-9]{3})[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4}))$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
if (matcher.matches()){
homePhone = value;
} else {
System.out.println(value + "is not a valid phone number.");
}
And then input the following test in my Main method (includes other fields):
AddressBook person3 = new AddressBook("Dark", "Wing", "Duck", "Duckville",
"123-245-6799", "123-49*51", "123-456-7891",
"@skypedarkdefender","letsgetdangerous", "www.darkwing.com");
which output:
Sorry; I'm still not sure what I'm doing wrong... but thanks for all the help to date!
Try using:
String regex = "^(([0-9]{3})[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4}))$";
Pattern p = Pattern.compile("^([0-9]{0,1}[-\\.\\s]?(\\d{3}|\\(\\d{3}\\))[-\\.\\s]?[0-9]{3}[-\\.\\s]?[0-9]{4})$");
Country code: No more than 1 digit (optional)
-,.,\\s (optional)
Open paren,3 digits Closed paren OR 3 digits
-,.,\\s (optional)
3 digits
-,.,\\s (optional)
4 digits
Just wanted to update to say I figured it out. I moved the regex out of my "set" method, and into the constructor:
private String firstName; // First name of the contact.
private String middleName; // Middle name of the contact.
private String lastName; //Last name of the contact.
private String homeAddress; //Contact home address.
private String businessPhone; //Contact business phone number.
private String homePhone; //Contact home phone number.
private String cellphone; //Contact cell phone number.
private String skypeId; // Contact Skype ID.
private String facebookId; //Contact Facebook ID.
private String personalWebSite; //Contact personal website URL.
/**
* Create a new instance of class AddressBook. Initialize instance variables
* to values of the arguments passed through the constructor parameters. All
* parameters defined through the variable list are defined in this
* instance.
*
* @param fn - First name of the contact.
* @param mn - Middle name of the contact.
* @param ln - Last name of the contact.
* @param homeAddress - Contact home address.
* @param businessPhone - Contact business phone number.
* @param homePhone - Contact home phone number.
* @param cellphone - Contact cell phone number.
* @param skypeId - Contact Skype ID.
* @param facebookId - Contact Facebook ID.
* @param personalWebSite - Contact personal website URL.
*/
public AddressBook(String fn, String mn, String ln,
String homeAddress, String businessPhone, String homePhone,
String cellphone, String skypeId, String facebookId,
String personalWebSite) {
this.businessPhone = businessPhone;
this.cellphone = cellphone;
this.facebookId = facebookId;
this.firstName = fn;
this.homeAddress = homeAddress;
this.homePhone = homePhone;
this.lastName = ln;
this.middleName = mn;
this.personalWebSite = personalWebSite;
this.skypeId = skypeId;
String regex = ("^([0-9]{0,1}[-\\.\\s]?(\\d{3}|\\(\\d{3}\\))[-\\.\\s]?[0-9]{3}[-\\.\\s]?[0-9]{4})$");
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(homePhone);
if (matcher.matches()) {
homePhone = homePhone;
} else {
homePhone = ("invalid phone number.");
}
System.out.println("First Name: " + fn);
System.out.println("Middle Name: " + mn);
System.out.println("Last Name: " + ln);
System.out.println("Home Phone: " + homePhone);
System.out.println("Business Phone: " + businessPhone);
System.out.println("Cellphone: " + cellphone);
System.out.println("Home Address: " + homeAddress);
System.out.println("Facebook ID: " + facebookId);
System.out.println("Skype ID: " + skypeId);
System.out.println("Personal Website: " + personalWebSite);
System.out.println();
}
Thanks all for the help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.