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\n"
+ "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\nName: %s %s\nEmail:
%s\n"
+ "Phone: %s\nZipCode: %s\n SSN: %s\nWage:%s\n\n",
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\n",
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\n");
} // 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
Please find my implementation:
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\n"
+ "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\nName: %s %s\nEmail: %s\n"
+ "Phone: %s\nZipCode: %s\n SSN: %s\nWage:%s\n\n",
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\n", 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\n");
} // end catch
} while (!valid);// end while
return outStr;
}
public String collectPhone(String number) {
String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
String outStr = "";
boolean valid = false;
System.out.println(number);
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\n");
} // 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 zip) {
String expression = "[0-9]{5}(?:-[0-9]{4})?$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
String outStr = "";
boolean valid = false;
System.out.println(zip);
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\n");
} // 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 ssn) {
String expression = "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
String outStr = "";
boolean valid = false;
System.out.println(ssn);
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\n");
} // 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) {
String expression = "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
double outStr = 0;
boolean valid = false;
System.out.println(messageIn);
do {
try {
outStr = input.nextDouble();
input.nextLine();
valid = true;
} catch (Exception e) {
System.out.printf("Please enter a valid double number\n");
} // end catch
} while (!valid);// end while
return outStr;
}
} // end class Validate
This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions...
Write an interface and two classes which will implements the interface. 1. Interface - StringVerification - will have the abstract method defined - boolean verifyInput( String input ); 2. class EmailVerification implements StringVerification - implement the verifyInput() method in EmailVerification class. The method should validate a user input String, character by character and see the input string has a dot (.) and an at the rate (@) character in it. If any of the condition wrong the verifyInput method will...
I am required to use the try - catch block to validate the input as the test data uses a word "Thirty" and not numbers. The program needs to validate that input, throw an exception and then display the error message. If I don't use the try - catch method I end up with program crashing. My issue is that I can't get the try - catch portion to work. Can you please help? I have attached what I have...
Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class Delete extends Info { String name; int Id; int del; public Delete() { } public void display() { Scanner key = new Scanner(System.in); System.out.println("Input Customer Name: "); name = key.nextLine(); System.out.println("Enter ID Number: "); Id = key.nextInt(); System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. "); del = key.nextInt(); if (del == 1) { int flag = 0; //learned...
Java need help with searching for a contact Question:Write a program that uses an ArrayList of parameter type Contact to store a database of contacts. The Contact class should store the contact’s first and last name, phone number, and email address. Add appropriate accessor and mutator methods. Your database program should present a menu that allows the user to add a contact, display all contacts, search for a specific contact and display it, or search for a specific contact and...
For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...
Hello Can you help to fix the program. When running, it still show Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Contact location: class ContactMap at ContactMap.main(ContactMap.java:40) C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) ************************ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ContactMap { public static void main(String args[]) throws IOException { Scanner input=new Scanner(System.in); //Create a TreeMap ,...
Write an application that displays a series of at least five student ID numbers (that you have stored in an array) and asks the user to enter a numeric test score for the student. Create a ScoreException class, and throw a ScoreException for the class if the user does not enter a valid score (less than or equal to 100). Catch the ScoreException, display the message Score over 100, and then store a 0 for the student’s score. At the...
Hello, I am continuously receiving errors in the code below. Can you please show me what I am doing wrong? import java.util.*; public class ShowTax { { public static void main(String [] args) { Tax myTax = new Tax(); String ssn, maritalStatus, lname, fname,zipcode; double income; int numberOfVehicles; Scanner s = new Scanner(System.in); String regex = "[0-9]{3}-[0-9]{2}-[0-9]{4}"; do { System.out.print("Enter the Social Security...
departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore { private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee; } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...
Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane { // checks customers in and assigns them a boarding pass // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers // public void reserveSeats() { int counter = 0; int section = 0; int choice = 0; String eatRest = ""; //to hold junk in input buffer String inName = ""; ...