English

Define a class to accept the gmail id and check for its validity. A gmail id is valid only if it has: → @ → . (dot) → gmail → com Example: [email protected] is a valid gmail id.

Advertisements

Question

Define a class to accept the gmail id and check for its validity.

A gmail id is valid only if it has:

→ @

→ . (dot)

→ gmail

→ com

Example: [email protected] is a valid gmail id.

Answer in Brief
Advertisements

Solution

import java.util.Scanner;
class EmailCheck
{
   public static void main(String args[])
   {
     Scanner sc=new Scanner(System.in); 
     System.out.println("Enter a valid gmail id"); 
     String mail=sc.next().trim();
     int c1=0,c2=0;
     for(int i=0;i<mail.length();i++) 
     {
       char c=mail.charAt(i); 
       if(c=='@')
       c1++;
       if(c=='.')
       c2++;
     }
     if(c1==1 && c2==1
     {
       int p=mail.indexOf('@');
       int q=mail.indexOf('.');
       String subdomain=mail.substring(p+1,q);
       String domain=mail.substring(q+1);
     if(subdomain.equals("gmail") && domain.
     equals("com"))
     System.out.println(mail+" is valid gmail id");
     else
       System.out.println(mail+" is an invalid gmail id"); 
   }
   else
       System.out.println(mail+"is an invalid gmail 
       id");
   }
}

Output:

Enter a valid gmail id

[email protected]

[email protected] is a valid gmail id

Enter a valid gmail id

[email protected]

[email protected] is an invalid gmail id

shaalaa.com
Using Function Argument
  Is there an error in this question or solution?
2023-2024 (March) Official
Advertisements
Share
Notifications



      Forgot password?
Use app×