Java: How do I do email validation (check if the e-mail contains@ or not)in java? It should be declared at the class level &should be taken in the parameter of a default constructor as wellwith a method in the class saying setEmail.
Answer
importjava.util.*;
class Email
{
private String email1;
// constructor
public Email()
{
this.email1 =“sample@g.com”;
}
// constructor
public Email(Stringemail1)
{
this.email1 =email1;
}
// return true if the email is valid, falseotherwise
public booleancheckIfvalid()
{
// matches functionmatches if the regular expression matches for a valid email
returnthis.email1.matches(“^[w-_.+]*[w-_.]@([w]+.)+[w]+[w]$”);
}
public void setEmail(Stringemail1)
{
this.email1 =email1;
}
public String getEmail()
{
returnthis.email1;
}
public static