Sameek Mishra
Sameek Mishra

Reputation: 9384

how to create a custom datatype in java?

I want to create an custom datatype in Java,for example datatype Email , that having following method isValidate(String email),isEmailExist(String email),getDomain(String email), get Id(String email),just like Integer class in java.

Integer is a class and I can initialise the object of Integer class as follows:

Integer i = 100;

I created my class Email and I want to initialise it as follows

Email e = "sam";

How can i perform this functionality in my Email class.

import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

Email() {  
}

public Boolean isvalid(String email)
{

String lastToken = null; Pattern p = Pattern.compile(".+@.+\.[a-z]+"); // Match the given string with the pattern Matcher m = p.matcher(email); // check whether match is found boolean matchFound = m.matches(); StringTokenizer st = new StringTokenizer(email, "."); while (st.hasMoreTokens()) { lastToken = st.nextToken(); }

if (matchFound && lastToken.length() >= 2 && email.length() - 1 != lastToken.length()) { return true; } else return false; } public String toString() { return email; } }

Thanks

Upvotes: 1

Views: 24698

Answers (2)

Viktor Mellgren
Viktor Mellgren

Reputation: 4506

You cannot instantiate it as you write, the closest would be using a constructor:

Email e = new Email("Sam");

Upvotes: 6

Dan Grossman
Dan Grossman

Reputation: 52372

Create an Email class. Java 101; any book or free tutorial of the Java language will get you started.

Upvotes: 3

Related Questions