Write an application that runs one time which prompts the user to input informat
ID: 3819292 • Letter: W
Question
Write an application that runs one time which prompts the user to input information about an acquaintance (first name, last name, phone number, email and relation). Your program should provide input checks with regular expression so that the user cannot input incorrect fields; when an incorrect field is input, the user should be prompted to enter a correct value for the field. Non-alphabetic character for the first name and last name are not permitted. The phone number can only be in the following formats:
1. 3193350297
2. 319-335-0297
3. 319.335.0297
4. (319)-335-0297
Accept email in the form (and lesser variations): john.Doe15@AOLSuper1.comgov
Relation can only be (in any caps): family, friend, or professional.
Sample Input Enter First Name: John Enter Last Name Doe Enter Phone Number 555t555t1234 Incorrect Phone Number K----- Enter Phone Number: 555-555-1234 Enter Email jo Congo Enter Relation (family, friend, professional): brother Incorrect Relation K----- Enter Relation (family, friend, professional): family output Name: John Doe Phone Number: 555-555-1234 Email: johnDoe15 AOLSuper1 com gov Relation: familyExplanation / Answer
import java.util.Scanner; public class Information { enum Relation { FAMILY("family"), FRIEND("friend"), PROFESSIONAL("professional"); String v; Relation(String value) { v = value; } public static Relation fromValue(String v) { for (Relation c : Relation.values()) { if (c.v.equals(v)) { return c; } } throw new IllegalArgumentException(v); } } public static void main(String[] args) { String firstName; String lastName; String phoneNumber; String emailId; String relation; Scanner scanner = new Scanner(System.in); boolean matches = false; do { System.out.println("Enter First Name:"); firstName = scanner.nextLine(); matches = firstName.matches("^.*[^a-zA-Z].*$"); // System.out.println(matches); if (matches) { System.out.println("**** Invalid First Name ****"); } } while (matches); do { System.out.println("Enter Last Name:"); lastName = scanner.nextLine(); matches = lastName.matches("^.*[^a-zA-Z].*$"); // System.out.println(matches); if (matches) { System.out.println("**** Invalid Last Name ****"); } } while (matches); do { System.out.println("Enter Phone Number:"); phoneNumber = scanner.nextLine(); matches = validatePhoneNumber(phoneNumber); System.out.println(matches); if (!matches) { System.out.println("**** Invalid Phone Number ****"); } } while (!matches); do { System.out.println("Enter Relation(family, friend, professional):"); relation = scanner.nextLine(); matches = true; try { Relation.fromValue(relation.toLowerCase()); } catch (IllegalArgumentException e) { matches = false; } if (!matches) { System.out.println("**** Invalid Realtion ****"); } } while (!matches); System.out.println("First Name : " + firstName); System.out.println("Last Name : " + lastName); System.out.println("Phone Number :" + phoneNumber); System.out.println("Relation:" + relation); } private static boolean validatePhoneNumber(String phNo) { //validate phone numbers of format "1234567890" if (phNo.matches("\d{10}")) return true; //validating phone number with -, . or spaces else if (phNo.matches("\d{3}[-\.\s]\d{3}[-\.\s]\d{4}")) return true; //validating phone number with extension length from 3 to 5 else if (phNo.matches("\d{3}-\d{3}-\d{4}\s(x|(ext))\d{3,5}")) return true; //validating phone number where area code is in braces () else if (phNo.matches("\(\d{3}\)-\d{3}-\d{4}")) return true; //return false if nothing matches the input else return false; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.