Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This Exercise contains two classes: ValidateTest and Validate . The Validate cla

ID: 3740948 • Letter: T

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 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

Explanation / Answer

/***** I have added methods for collectPhone, collectZip, collectSsn and collectDouble and tested with regular expression. Let me know from your end*****? /

package learning;

import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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
      */
    
     public String collectPhone(String messageIn) {
         String expression = "^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{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 phone number ");
             } // end catch
         } while (!valid);// end while
         return outStr;
     }
    
    

     /** 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.
      */
    
     public String collectZip(String messageIn) {
         String expression = "^\d{5,}$";
         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 Zip number ");
             } // end catch
         } while (!valid);// end while
         return outStr;
     }
    
    
    
    /** 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.
      */
    
     public String collectSsn(String messageIn) {
         String expression = "^\d{3}[- ]?\d{2}[- ]?\d{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 SSN ");
             } // end catch
         } while (!valid);// end while
         return outStr;
     }
    
     /** 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
      */
    
     public double collectDouble(String messageIn) {
         double doubleOut = 0.0;
         boolean valid = false;

        System.out.println(messageIn);
         do {
             try {
            doubleOut = input.nextDouble();
                 input.nextLine();
                 valid = true;
             } catch (InputMismatchException e) {
                 input.nextLine();
                 System.out.println("This requires a double number!");
             } // end catch
         } while (!valid);
         return doubleOut;
     }
    

} // end class Validate

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote