KnowledgeBoat Logo

Computer Applications

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: icse2024@gmail.com is a valid gmail id

Java

Java String Handling

ICSE 2024

6 Likes

Answer

import java.util.Scanner;

public class KboatValidateGmail
{
    public static void main(String args[]) 
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a Gmail ID: ");
        String gmailId = in.nextLine();
        int len = gmailId.length(); 

        if (gmailId.endsWith("@gmail.com")
        && gmailId.indexOf('@') == len - 10)
        {
            System.out.println(gmailId + " is a valid Gmail ID.");
        }
        else
        {
            System.out.println(gmailId + " is NOT a valid Gmail ID.");
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of 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: icse2024@gmail.com is a valid gmail idBlueJ output of 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: icse2024@gmail.com is a valid gmail idBlueJ output of 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: icse2024@gmail.com is a valid gmail id

Answered By

3 Likes


Related Questions