一道筆試題


今天去面試了一家外資公司,遇到一道筆試題

題目大概內容如下:

題目:判斷郵箱地址是否為正確格式,如:aaa@foxmail.com;ABcd@sina.com.cn;
郵箱中除了"@"和".",其余字符全為字母。不要使用正則表達式,寫出思路或畫出流程圖

下面是我整理出來的代碼:

 1 package com.b510.util;
 2 
 3 /**
 4  * 判斷郵箱地址是否為正確格式,如:aaa@foxmail.com;ABcd@sina.com.cn; 郵箱中除了"@"和".",其余字符全為字母。
 5  * 
 6  * @author hongten
 7  * @date 3013-06-05
 8  * 
 9  */
10 public class CheckEmail {
11 
12     public static void main(String[] args) {
13         String email = "hongtenfoxmail.com.cn";
14         boolean isEmail = checkEmail(email);
15         String result = isEmail ? "正確" : "不正確";
16         System.out.println("郵箱 [" + email + "]格式是否正確:" + result);
17 
18     }
19 
20     /**
21      * 判斷郵箱地址是否為正確格式,如:aaa@foxmail.com;ABcd@sina.com.cn; 郵箱中除了"@"和".",其余字符全為字母。
22      * 
23      * @param email
24      * @return
25      */
26     public static boolean checkEmail(String email) {
27         boolean indexFlag = false;
28         boolean lastFlag = false;
29         String[] strs = email.split("@");
30         System.out.println(strs.length);
31         // 之前代碼為:<code>if (strs.length > 0 && strs.length <= 2) {</code>,謝謝
32         // <a href="http://home.cnblogs.com/u/535203/">鋼板</a>提出的bug
33         if (email.lastIndexOf("@") != -1 && strs.length <= 2) {
34             String index = strs[0];
35             String last = strs[1];
36             indexFlag = isAllChars(index);
37             String[] lastArray = last.split("\\.");
38             for (int j = 0; j < lastArray.length; j++) {
39                 if (lastArray[j] == null || lastArray[j].equals("")) {
40                     // 說明有".foxmail...com."這種情況
41                     lastFlag = false;
42                     break;
43                 } else {
44                     lastFlag = isAllChars(lastArray[j]);
45                     if (!lastFlag) {
46                         break;
47                     }
48                 }
49             }
50             return (indexFlag && lastFlag);
51         } else {
52             return false;
53         }
54     }
55 
56     /**
57      * 判斷一個字符串是否全為字母,此方法中,不管str中包含是否包含非字母字符,都會把str整個遍歷
58      * 
59      * @param str
60      *            所需判斷的字符串
61      * @return
62      */
63     public static boolean isAllChar(String str) {
64         if (str == null || str.equals("")) {
65             return false;
66         }
67         int j = 0;
68         for (int i = 0; i < str.length(); i++) {
69             if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
70                     || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {
71                 j = j + 1;
72             }
73         }
74         return (j == str.length());
75     }
76 
77     /**
78      * 判斷一個字符串是否全為字母,此方法比上面的isAllChar方法效率要高,但是需要的是str中包含非字母字符在靠前面
79      * 如:"a2bcd",對於這個字符串,到字符"2"的時候就會終止判斷
80      * 
81      * @param str
82      *            所需判斷的字符串
83      * @return
84      */
85     public static boolean isAllChars(String str) {
86         if (str == null || str.equals("")) {
87             return false;
88         }
89         boolean flag = true;
90         for (int i = 0; i < str.length(); i++) {
91             if ((str.charAt(i) < 'a' || str.charAt(i) > 'z')
92                     && (str.charAt(i) < 'A' || str.charAt(i) > 'Z')) {
93                 flag = false;
94                 break;
95             }
96         }
97         return flag;
98     }
99 }

運行結果:

郵箱 [hongten@foxmail.com]格式是否正確:正確
郵箱 [hongten@foxmail..com.]格式是否正確:不正確
郵箱 [h2ongten@foxmail.com]格式是否正確:不正確
郵箱 [hongten@foxma2il.com]格式是否正確:不正確
郵箱 [@foxmail.com]格式是否正確:不正確
郵箱 [hon.gten@foxmail.com]格式是否正確:不正確
郵箱 [hongten@foxmail.com.cn]格式是否正確:正確

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM