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

Using Java, This Exercise contains two classes: ValidateTest and Validate . The

ID: 3805188 • Letter: U

Question

Using Java, 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. 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}$"

PREVIOUSLY STARTED CODE:

import java.util.*;
import java.util.regex.*;

/**
* Created:
* Author:
* FileName:
*/

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 **
//************************************************************
class Validate {

private Scanner input;

public Validate() {
input = new Scanner(System.in);
}

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

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

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

Here is the working solution-

//Note: you mentioned above about only three method but you asked implementation of four method's below.

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

public class Validate {
   private Scanner input;

   public Validate() {
       input = new Scanner(System.in);
   }

   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

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

   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{1,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 code ");
           } // 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 number ");
           } // 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;
       boolean valid = false;
       System.out.println(messageIn);
       do {
           try {
               doubleOut = input.nextDouble();
               if(doubleOut == Math.floor(doubleOut)){
                   throw new InputMismatchException();
               }
               input.nextLine();
               valid = true;
           } catch (InputMismatchException e) {
               input.nextLine();
               System.out.println("This requires a double value like 15.5!");
           } // end catch
       } while (!valid);
       return doubleOut;
   } // end collectdouble


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