Email validation – java code

This is a java method to validate the email address. I used this application in struts (J2EE).
We can also use it in jsp, with little bit modification.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidator
{
public boolean eMail(String email)
{
String at=”@”;
String dot=”.”;
int lat=email.indexOf(at);
int lstr=email.length();
Pattern p = Pattern.compile(“.+@.+\\.[a-z]+”);

Matcher m = p.matcher(email);
boolean matchFound = m.matches();

// check if ‘.’ is at the first position or at last position or absent in given email
if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 ||
email.indexOf(dot)==lstr)
{
matchFound =false;
return matchFound;
}

// check if ‘@’ is used more than one times in given email
if (email.indexOf(at,(lat+1))!=-1)
{
matchFound =false;
return matchFound;
}

// check for the position of ‘.’
if (email.substring(lat-1,lat)==dot ||email.substring(lat+1,lat+2)==dot)
{
matchFound =false;
return matchFound;
}

// check if ‘.’ is present after two characters from location of ‘@’
if (email.indexOf(dot,(lat+2))==-1)
{
matchFound =false;
return matchFound;
}

// check for blank spaces in given email
if (email.indexOf(” “)!=-1)
{
matchFound =false;
return matchFound;
}
return matchFound;
}
}

Here I took a boolean value to get the result. If my result is false then I am generating the one message(email is not valid).

If my result is true, then action will happens.

//referred by roseindia.net

Convert Date to simple date format as dd/mm/yyyy

Here I specified one of the easiest way of converting the Date (yy:mm:dd to mm:dd:yy).
This is free defined method . I am importing java.text.SimpleDateFormat, java.sql.Date in my jsp.
In my mysql database contains the date, its in date format ,
I am retrieving as a date in my java class like
Date x= rs.getDate(“created_date”);,
created_date is a database field where my Date exists.
And in my jsp page.
Here is jsp process:
to is a object, my all data are stored in that object.
Date date1=to.getDate1();
SimpleDateFormat formatter = new SimpleDateFormat (“dd/MM/yyyy”);
String s1 = formatter.format(date1);
Just converting it to String and assigning the date value.
I hope its a very simpler date converting formula.

To generate the random numbers(method)

This java methods helps to generate the five digit random numbers .

import java.util.Random;
public class RondomNumberGenerator {
public int generateNumbers(int x)
{

Random generator = new Random();
int random_digit = new Double( Math.random() * 100000 ).intValue();
return random_digit;
}
}

This is the method class, if you want to generate the random number, just call this method. In your java class. You have to import the RondomNumberGenerator and then call this method.

After importing the RondomNumberGenerator ,
Call the method like this:
RondomNumberGenerator generate = new RondomNumberGenerator();
int x1=0;
int y=generate.generateNumbers(x1);
Y contains the five digit random number.
Enjoy the five digit random number.

The code for converting integr to float and convert to percentage

int posivitivecount=12;

int totalcount=40;

float percentage=(float)posivitivecount/totalcount*100;

Now the value is converted is a float, if you want to fix the decimal places, you have to use the one more method

DecimalFormat dec = new DecimalFormat(“###.#”);

import the method before you call. That is java.text.DecimalFormat,

after inserting this method, pass the float value and convert it to string, and then convert to integer. if positive not start with decimal , then add . and 0 to that.

you will get the result like 12.0, 12.9.. like that. To do that.

String positiverate=dec.format(percentage);
int pos=positiverate.lastIndexOf(“.”);
if(!positiverate.startsWith(“.”,pos))
positiverate=positiverate+”.0″;