IP地址合法性檢查代碼
private static String IPComp = "0123456789.";public void CheckIP(String IP){
int i;
int index;
String cur;
//判斷是否存在非IP字符
for (i = 0;i < IP.length();i++){
if (IPComp.indexOf(IP.charAt(i)) == -1){
throw new yourException(yourErrorString);
}
}
//變換IP為'xxx.'重復的格式
IP = IP + ".";
//分別獲得前四個'xxx.'檢查
for (i = 1;i <= 4;i++){
//判斷底i個'xxx.'是否存在(i<=4)
index = IP.indexOf('.');
if (index == -1){
throw new yourException(yourErrorString);
}
else{
//獲得數字,檢查數字范圍
cur = IP.substring(0,index);
IP = IP.substring(index + 1);
if (cur.length() > 0 && cur.length() <= 3){
//change cur to int
int num = 0;
for (index = 0;index < cur.length();index++){
num = num * 10 + (int)cur.charAt(index) - 48;
}
if (num > 255){
throw new yourException(yourErrorString);
}
}
else{
throw new yourException(yourErrorString);
}
}
}
//檢查是否有多的'xxx.'
if (IP.indexOf('.') != -1){
throw new yourException(yourErrorString);
}
}
posted on 2005-07-27 17:50 蘑菇 閱讀(1265) 評論(0) 編輯 收藏 所屬分類: 代碼實現--JAVA