Email checking, but better
People were so pleased with your email checker that they want it improved to fit even more needs. You were made aware that not all email finish with 3 characters, and therefore try to fit that into the code. Also, you've been asked to add a message indicating when the @
symbol is missing.
This exercise is part of the course
Intermediate Java
Exercise instructions
- Add a logical operator to check if there is a
".""
anywhere after the"@"
. - Use the right control flow to catch all bad emails - you want to flag emails that have
"@"
at the right or don't contain"."
after"@"
. - Make sure, using the right logical operator, that we get a message for emails not containing
@
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class EMailChecker {
public static void main(String[] args) {
String adrs = "[email protected]";
int addLen = adrs.length();
boolean hasAt = adrs.contains("@");
if (hasAt && adrs.charAt(addLen - 4) == '.') {
System.out.println("Send that email !");
// Enter the correct logical operator to be able to catch all correct emails
} else if (hasAt && (adrs.charAt(addLen - 3) == '.' ____ hasDotAfterAt(adrs))) {
System.out.println("That's a correct email address");
// Use the correct keyword to catch any bad email addresses
} ____ {
// Make sure that the users know when the '@' is missing
if (____hasAt) {
System.out.println("Your email is missing the '@'");
} else {
System.out.println("That's not a valid email");
}
}
}
static boolean hasDotAfterAt(String address) {
int atPos = address.indexOf('@');
String subString = address.substring(atPos);
return subString.contains(".");
}
}