Write a java program to read a string and validate the given email-id as input.
Validation Rules:
1. Ensure that there are at least 5 characters between ‘@’ and ‘.’
2. There should be only one ‘.’ and one ‘@’ symbol.
3. The ‘.’ should be after the ‘@’ symbol.
4. There must be at least three characters before ‘@’.
5. The string after ‘.’ should only be ‘com’
Input and Output Format:
Input consists of a string.
The output consists of TRUE / FALSE.
Refer sample output for formatting specifications.
Sample Input 1:
test@gmail.com
Sample Output 1:
TRUE
Sample Input 2:
academy@xyz.com
Sample Output 2:
FALSE
E-Mail Validation in Java.
import java.util.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ip = sc.nextLine();
boolean b = emailIdValidation(ip);
if (b == true)
System.out.println("TRUE");
else
System.out.println("FALSE");
}
public static boolean emailIdValidation(String ip) {
int i = 0;
boolean b = false;
StringTokenizer t = new StringTokenizer(ip, "@");
String s1 = t.nextToken();
String s2 = t.nextToken();
StringTokenizer t1 = new StringTokenizer(s2, ".");
String s3 = t1.nextToken();
String s4 = t1.nextToken();
if (ip.contains("@") && ip.contains("."))
i++;
if (i == 1)
if (s3.length() == 5)
if (s1.length() >= 3)
if (s4.equals("com"))
b = true;
return b;
}
}