I'm new in programming and need to validate this class email.
Anybody can help?

public class Email {

    private String email;

    public Email() {
        email = "";
    }

    public Email(String emailAddress) {
        this.email = emailAddress;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Edited 3 Years Ago by Dani: Formatting fixed

Little confused by this request...
Your class is OK, but you may want to change setter access from public to private. You already provided user with way to set email value through constructor public Email(String emailAddress) .
As for validation you need to check out if submitted string contains "@" and dot "." after it example test@test.com. That would be the simplest validation process. In real life you would want to send email to entered address and receive confirmation from owner so you are most likely have something like unconfirmed and confirmed state that can be addressed in various ways.
Is this answer you been looking for or was it something else?

Little confused by this request...
Your class is OK, but you may want to change setter access from public to private. You already provided user with way to set email value through constructor public Email(String emailAddress) .
As for validation you need to check out if submitted string contains "@" and dot "." after it example test@test.com. That would be the simplest validation process. In real life you would want to send email to entered address and receive confirmation from owner so you are most likely have something like unconfirmed and confirmed state that can be addressed in various ways.
Is this answer you been looking for or was it something else?

I've the class Diagram as:
Email
________
-email : String
__________
+Email()
+Email(emailAddress : String)
+setEmail(email : String) : void
+getEmail() : String
+validEmail(address : String) : boolean

well then you need only write this new method validEmail(String) with functionality as given in my previous reply and result of the operation return as a boolean type

Like that?

public  boolean validEmail(String address) {
        if (email == null) {
            return false;
        }
        //Assigning the email format regular expression
        address = "^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})";
        return email.matches(address);
    }

Edited 3 Years Ago by Dani: Formatting fixed

Like that?

public  boolean validEmail(String address) {
        if (email == null) {
            return false;
        }
        //Assigning the email format regular expression
        address = "^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})";
        return email.matches(address);
    }

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.