Question
This Exercise contains two classes: ValidateTest and Validate . The Validate class uses try-catch and Regex expressions in methods to test data passed to it
This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions in methods to test data passed to it from the ValidateTest program. Please watch the related videos listed in Canvas and finish creating the 3 remaining methods in the class. The regular expression you will need for a Phone number is: "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$" and for an SSN: "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$" This exercise is meant to be done with the video listed in the Assignment page for Week 10.
What I have so far:
import java.util.*; import java.util.regex.*;
/** * Enter JavaDoc Class information here * Include a description of what this class does!!! * Make sure you include your full name * Filename: ValidateTest.java */
public class ValidateTest {
public static void main(String[] args) { Validate validate = new Validate();
int id = validate.collectInt("Please enter the ID of the item:"); String firstName = validate.collectString(2, "Please enter the first " + "name of the individual"); String lastName = validate.collectString(3, "Please enter the last " + "name of the individual"); String email = validate.collectEmail("Please enter the email address"); String phone = validate.collectPhone("Please enter the phone Number " + "Like (123) 456-7890 or 1234567890 or 123-456-7890"); String zipcode = validate.collectZip("Please enter the zipcode"); String ssn = validate.collectSsn("Please enter the SSN"); double wage = validate.collectDouble("Please enter the hourly wage");
System.out.print(String.format("id: %s Name: %s %s Email: %s " + "Phone: %s ZipCode: %s SSN: %s Wage:%s ", id, firstName, lastName, email, phone, zipcode, ssn, wage));
} // end main method
} // end class ValidateTest
//************************************************************ //** Validate Class is below this box ** //************************************************************ /** * Enter JavaDoc Class information here * Include a description of what this class does!!! * Make sure you include your full name * Filename: Validate.java */
class Validate {
private Scanner input;
/** * JavaDoc method comment here * */ public Validate() { input = new Scanner(System.in); } /** * enter JavaDoc Comments */ public int collectInt(String messageIn) { int intOut = 0; boolean valid = false;
System.out.println(messageIn); do { try { intOut = input.nextInt(); input.nextLine(); valid = true; } catch (InputMismatchException e) { input.nextLine(); System.out.println("This requires a whole number!"); } // end catch } while (!valid); return intOut; } // end collectInt /** * enter JavaDoc comments */ public String collectString(int strLen, String messageIn) { String outStr = ""; boolean valid = false;
System.out.println(messageIn); do { try { outStr = input.nextLine(); if (outStr.length() < strLen) { throw new Exception(); } valid = true; } catch (Exception e) { System.out.printf("This requires more than %d charachters ", strLen); } // end Catch } while (!valid); return outStr; }
/** * enter JavaDoc comments */ public String collectEmail(String messageIn) { String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$"; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
String outStr = ""; boolean valid = false; System.out.println(messageIn); do { try { outStr = input.nextLine();
Matcher m = pattern.matcher(outStr); if (!m.matches()) { throw new Exception(); } valid = true; } catch (Exception e) { System.out.printf("Please enter a valid email address "); } // end catch } while (!valid);// end while return outStr; } /** create collectPhone method here using the supplied phone regex expression * that, like the collectEmail method, tests the pattern and returns the a * phone number string */
/** create a collectZip method here. It can be done by checking for an integer * that's at least 5 characters long or reading up on regex expressions and * using your own regex expression to create the test pattern. */
/** create collectSsn method here that like the collectEmail method, testa a * string to make sure it matches a social security number based on the * regex expression. */ /** create collectdouble method here that, like the collectInt method, tests * the a string to make sure it is a double value or requests the value again * A double value would be like 15.55 */
} // end class Validate
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started