Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Intermediate Java

Cursus bekijken

Oefeninstructies

  • 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 @.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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(".");
  }
}
Code bewerken en uitvoeren