Question

In: Computer Science

In the second task, you will write a Java program that validates an input to see...

In the second task, you will write a Java program that validates an input to see whether it matches a specified pattern. You will have to read a bit about regular expressions for this Lab.

1. Prompt the user to enter the email address.

2. Read the user input as a String.

3. A regular expression that specifies the pattern for an email address is given by “[a-zA-Z0- 9.]++@[a-zA-Z]++.(com|ca|biz)”. Use this as the input to the matches method in the String class and output the result returned by the method.

4. Prompt the user to enter the full name.

5. Full name should have First name, zero or Middle names and the Last name. For this purpose, we will assume a name (first, middle or last) is a set of characters without any spaces. Some examples are “John”, “McDonald”, “smiTh”. The names are separated by exactly one space. Write a regular expression to represent this pattern.

6. Use the matches method in the String class to validate the name entered by the user and output the result returned by the method.

A possible dialogue with the user might be;

Please enter the email address: [email protected]

true

Please enter the full name: Malaka Second Third Walpola

true

Solutions

Expert Solution

if you have any doubts, please give me comment...

import java.util.Scanner;

public class Validations{

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);

        boolean isValid;

        System.out.print("Please enter the email address: ");

        String email = scnr.nextLine();

        isValid = email.matches("[a-zA-Z0-9.]++@[a-zA-Z]++.(com|ca|biz)");

        System.out.println(isValid);

        System.out.print("Please enter the full name: ");

        String fullName = scnr.nextLine();

        isValid = fullName.matches("^[a-zA-Z]+(?: [a-zA-Z]+)* ?$");

        System.out.println(isValid);

    }

}


Related Solutions

Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
Write a java program. The program requirements are: the program will let the user input a...
Write a java program. The program requirements are: the program will let the user input a starting number, such as 2.  It will generate ten multiplication problems ranging from 2x1 to 2x10.  For each problem, the user will be prompted to enter the correct answer. The program should check the answer and should not let the user advance to the next question until the correct answer is given to the question being asked currently. After testing a total of 10 multiplication problems,...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
JAVA script! You will write a program that will input the nationalities of your favorite types...
JAVA script! You will write a program that will input the nationalities of your favorite types of food, examples, and prices of some of your favorite food. You will save a type of food, two examples of that type of food, the price of each of those two examples, and a total price of both together. You must input your own types, examples, and prices, but check a sample run below. You must use the concepts and statements that we...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT