HERE IS THE CODE:
private static boolean isLegalCity(String city) {
return city.matches("([A-Z]+[a-z]+|\\s[A-Z]+)*");
}
private static boolean isLegalPhoneNumber(String phone){
return phone.matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}");
}
HERE IS THE COMPLETE CODE:
mport java.util.Scanner;
/**
*
* @author mike
*/
public class Lab20 {
private static Scanner scan;
private static String fName, lName, sAddress, city, state, zip, phone;
/**
* Check for legal first name
* @param fName
* @return first name legal or not
*/
private static boolean isLegalFirstName(String fName) {
return fName.matches("[A-Z][a-zA-z]*");
}// end of isLegalFirstName
/**
* Check for legal last name
* @param lName
* @return last name is legal or not
*/
private static boolean isLegalLastName(String lName) {
return lName.matches("[a-zA-z]+([ '-][a-zA-Z]+)*");
}// end of isLegalLastName
/**
* Check for legal street adress
* @param sAdress
* @return street is legal or not
*/
private static boolean isLegalStreetAddy(String sAdress) {
return sAdress.matches("\\d+\\s+([A-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");
}// end of isLegalStreetAddy
/**
* Check for legal city entry
* @param city
* @return legal city or not
*/
private static boolean isLegalCity(String city) {
return city.matches("([A-Z]+[a-z]+|\\s[A-Z]+)*");
}// end of isLegalCity
/**
* Check for legal state
* @param state
* @return legal state or not
*/
private static boolean isLegalState(String state) {
return state.matches("([A-Z]+[A-Z])*");
}// end of isLegalState
/**
* Check for legal zip
* @param zip
* @return legal zip or not
*/
private static boolean isLegalZip(String zip) {
return zip.matches("\\d{5}");
}// end of isLegalZip
/**
* Check for legal phone number
* @param phone
* @return phone number is legal or not
*/
private static boolean isLegalPhoneNumber(String phone){
return phone.matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}");
}// end of isLegalPhoneNumber
/**
* Display either all valid or specify the invalid entries
* @param fName first name
* @param lName last name
* @param street street address
* @param city
* @param state
* @param zip
* @param phone
*/
private static void dispValidString(String fName, String lName, String street,
String city, String state, String zip, String phone) {
String invalid = "";
if (!isLegalFirstName(fName)) {
invalid += "Invalid First Name!\n";
}// end of if
if (!isLegalLastName(lName)) {
invalid += "Invalid Last Name!\n";
}// end of if
if (!isLegalStreetAddy(street)) {
invalid += "Invalid Street Address!\n";
}// end of if
if (!isLegalCity(city)) {
invalid += "Invalid City!\n";
}// end of if
if (!isLegalState(state)) {
invalid += "Invalid State!\n";
}// end of if
if (!isLegalZip(zip)) {
invalid += "Invalid Zip Code!\n";
}// end of if
if (!isLegalPhoneNumber(phone)){
invalid += "Invalid phone number!\n";
}// end of if
if (isLegalFirstName(fName) && isLegalLastName(lName)
&& isLegalStreetAddy(sAddress) && isLegalCity(city)
&& isLegalState(state) && isLegalZip(zip) &&
isLegalPhoneNumber(phone)) {
invalid += "Nothing was invalid.\nAll fields were entered "
+ "correctly!";
}// end of if
// display the results
System.out.println(invalid);
}// end of dispString
/**
* Main method
* @param args command line entry, not used
*/
public static void main(String[] args) {
// instanciate scanner for user input
scan = new Scanner(System.in);
// get user inputs and store values
System.out.print("Enter your first name: ");
fName = scan.nextLine();
System.out.print("Enter your last name: ");
lName = scan.nextLine();
System.out.print("Enter your street address: ");
sAddress = scan.nextLine();
System.out.print("Enter your city: ");
city = scan.nextLine();
System.out.print("Enter your state: ");
state = scan.nextLine();
System.out.print("Enter your zip code: ");
zip = scan.nextLine();
System.out.println("Enter your phone number: ");
phone = scan.nextLine();
// diplay message
dispValidString(fName, lName, sAddress, city, state, zip, phone);
}// end of main
}// end of Lab20 class
Thanks in advance


Sign In
Create Account


Back to top









