/** * COMP 1711 (Fall 2005) * * Solution to Challenge Problem #4 (Spam Helper) * * This class has a static method that takes a potential e-mail address * and tests to see if it meets the standards in the problem description. * * @author Based on a solution by Andy Hebert * @version Dec-08-2005 */ public class Problem04 { //--------------------------------------------------------------------------- /** * This method takes in a potential address and determines if it is valid. * * @param test The email address to test (a String) */ public static boolean isValidEmail(String test) { String lowerTest = test.toLowerCase(); boolean testAt = rightAts(lowerTest); boolean testDot = rightDots(lowerTest); boolean testChar = rightChars(lowerTest); if (testAt && testDot && testChar) { return true; } // if return false; } // end isValidEmail(String) //--------------------------------------------------------------------------- /** * Private method to test that the input String contains exactly one '@' * character, and that this character is neither the first nor the last. * * @param at Potential e-mail address to test (a String) */ private static boolean rightAts(String at) { int size = at.length(); int firstAt = at.indexOf('@'); // find position of first occurrence of '@' int lastAt = at.lastIndexOf('@'); // find position of last occurrence of '@' if(firstAt == -1 || (firstAt != lastAt) || firstAt == 0 || lastAt == (size-1)) { return false; } // if return true; } // end rightAts(String) //--------------------------------------------------------------------------- /** * Private method to test that the input String has any '.' characters * positioned correctly. This means that '.' cannot be the first or last * character, and that the substrings "..", ".@", and "@." cannot occur. * * @param dots Potential e-mail address to test (a String) */ private static boolean rightDots(String dots) { int size = dots.length(); int firstDot = dots.indexOf('.'); // find position of first occurrence of '.' int lastDot = dots.lastIndexOf('.'); // find position of last occurrence of '.' int bad1 = dots.indexOf(".."); // find position of first occurrence of substring ".." int bad2 = dots.indexOf("@."); // find position of first occurrence of substring ".@" int bad3 = dots.indexOf(".@"); // find position of first occurrence of substring "@." if(firstDot == 0 || lastDot == (size - 1) || (bad1 >= 0) || (bad2 >= 0) || (bad3 >= 0)) { return false; } // if return true; } // end rightDots(String) //--------------------------------------------------------------------------- /** * Private method to test that all characters in the input String that * are not '@' or '.' are uppercase letters, lowercase letters, or digits. * * @param chars Potential e-mail address to test (a String) */ private static boolean rightChars(String chars) { int size = chars.length(); char ch; for(int i = 0; i < size; i ++) { ch = chars.charAt(i); if ((ch != '.') && (ch != '@')) { boolean isOK = (('a' <= ch) && (ch <= 'z')) || (('0' <= ch) && (ch <= '9')); if (!isOK) { return false; } // if } // if } // for i return true; } // end rightChars(String) //--------------------------------------------------------------------------- }// end class Problem04