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;
    }
}

Recommended Answers

All 6 Replies

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);
    }

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);
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.