Write a method, isEmailAddress, that is passed a String argument.

1
Write a method, isEmailAddress, that is passed a String argument.

LANGUAGE: Java

CHALLENGE:

Write a method, isEmailAddress, that is passed a String argument.
It returns true or false depending on whether the argument has the form of an email address.
In this exercise, assume that an email address has one and only one “@” sign and no spaces, tabs or newline characters.

SOLUTION:

public static boolean isEmailAddress(String s)
{
    int space=0,dot=0,atTheRate=0;
    int i;
    for(i=0;i<s.length();i++)
    {
        if(s.charAt(i)==' ')space++;
        if(s.charAt(i)=='.')dot++;
        if(s.charAt(i)=='@')atTheRate++;
    }
    if(space==0 && dot>=1 && atTheRate==1)return true;
    else return false;
}

One comment

  • shusma kafle says:

    public static boolean isEmailAddress (String aString)
    {
    if (aString.indexOf(“@”) != -1)
    {
    if (aString.indexOf(“@”) == aString.lastIndexOf(“@”))
    {
    if (aString.indexOf(” “) == -1)
    {
    if (aString.indexOf(“\n”) == -1)
    {
    if (aString.indexOf(“\t”) == -1)
    {
    return true;
    }
    }
    }
    }
    }
    return false;
    }

Add comment