Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java Based Programming //EXAMPLE CODE THAT NEEDS TO BE EDITED public class ValidateInput { // validate first name public static boolean validateFirstName(String firstName) { return
Java Based Programming
//EXAMPLE CODE THAT NEEDS TO BE EDITED
public class ValidateInput { // validate first name public static boolean validateFirstName(String firstName) { return firstName.matches("[A-Z][a-zA-Z]*"); } // validate last name public static boolean validateLastName(String lastName) { return lastName.matches("[a-zA-z]+(['-][a-zA-Z]+)*"); } // validate address public static boolean validateAddress(String address) { return address.matches( "\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)"); } // validate city public static boolean validateCity(String city) { return city.matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)"); } // validate state public static boolean validateState(String state) { return state.matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)"); } // validate zip public static boolean validateZip(String zip) { return zip.matches("\\d{5}"); } // validate phone public static boolean validatePhone(String phone) { return phone.matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}"); } } // end class ValidateInput
//SECOND EXAMPLE CODE THAT NEEDS TO BE EDITED FOR APPLICATION CLASS
public class Validate2 { public static void main(String[] args) { // get user input Scanner scanner = new Scanner(System.in); boolean allValid; // validate user input and display error message System.out.println(" Validate Result:"); do { allValid = true; System.out.println("Please enter first name:"); String firstName = scanner.nextLine(); System.out.println("Please enter last name:"); String lastName = scanner.nextLine(); System.out.println("Please enter address:"); String address = scanner.nextLine(); System.out.println("Please enter city:"); String city = scanner.nextLine(); System.out.println("Please enter state:"); String state = scanner.nextLine(); System.out.println("Please enter zip:"); String zip = scanner.nextLine(); System.out.println("Please enter phone:"); String phone = scanner.nextLine(); if (!ValidateInput.validateFirstName(firstName)) { System.out.println("Invalid first name"); allValid = false; } if (!ValidateInput.validateLastName(lastName)) { System.out.println("Invalid last name"); allValid = false; } if (!ValidateInput.validateAddress(address)) { System.out.println("Invalid address"); allValid = false; } if (!ValidateInput.validateCity(city)) { System.out.println("Invalid city"); allValid = false; } if (!ValidateInput.validateState(state)) { System.out.println("Invalid state"); allValid = false; } if (!ValidateInput.validateZip(zip)) { System.out.println("Invalid zip code"); allValid = false; } if (!ValidateInput.validatePhone(phone)) { System.out.println("Invalid phone number"); allValid = false; } } while(!allValid); System.out.println("Valid input. Thank you."); } } // 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