이메일 검사, 더 개선하기
여러분이 만든 이메일 검사기가 큰 호응을 얻어, 더 다양한 요구 사항을 반영해 달라는 요청이 들어왔어요. 모든 이메일이 3글자로 끝나는 것은 아니라는 점을 반영해 코드를 수정해 주세요. 또한 @ 기호가 없을 때 이를 알려 주는 메시지도 추가해 달라고 요청받았습니다.
이 연습은 강의의 일부입니다
중급 Java
연습 안내
- 논리 연산자를 사용해
"@"이후 어디에든".""가 있는지 확인하세요. - 올바른 제어 흐름을 사용해 잘못된 이메일을 모두 잡아내세요.
"@"가 오른쪽 끝에 있거나 또는"@"뒤에"."가 없는 이메일을 표시해야 합니다. - 적절한 논리 연산자를 사용해
@가 없는 이메일에 대해 메시지가 출력되도록 하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
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(".");
}
}