help me with this simple java problem...?

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.

my code is:

public boolean isEmailAddress (String argument)

{

boolean test = false;

if (argument.indexOf('@') != -1 )

test = false;

else

test = true;

return test;

}

what I dont understand is how do I find only one @ character,and new line, etc

Update:

public boolean isEmailAddress (String argument)

{

boolean test = false;

boolean test1 = false;

int yes = 0;

for (int i = 0; i<argument.length()-1; i++)

{

if (argument.charAt(i) == '@')

yes++;

}

for (int i = 0; i<argument.length()-1; i++)

{

if (argument.charAt(i) == ' ' && argument.charAt(i) == '\t' && argument.charAt(i) == '\n')

test1 = false;

else

test1 = true;

}

if (yes != 1 && test1 == false)

test = false;

else if (yes == 1 && test1 == true)

test = true;

return test;

}

that is my code above, why is it still wrong?

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    The best way is to always write your logic / program and dont depend on the methods provided in the language all the time. Now what you can do is!

    1-Change the string to a character array "

    > char[] tempArray = new char[99];

    > tempArray = String.toCharArray(enteredEmail);"

    2-Iterate through the characters using for or while or any loop you like and match each character from [0]-[99] this is just an illustration that the email could be 99 characters long. Have a varible set to zero before the loop starts and for each check put another variable. Whenever you hit the character that you want to match with your if statement increment the corrsponding varaible. And at the end of the loop you will know that how many time the @ symbol has appeared or the '.' symbol has appeared.

    Hope this helps! Good luck!!

    • carbo1165 years agoReport

      it does not help

    • Commenter avatarLogin to reply the answers
  • Anonymous
    1 decade ago

    You need to look up information on REGEXP and understand them better. When you understand them properly, you can make this routine using one line of code.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.