随笔-9  评论-15  文章-1  trackbacks-0
在工作中碰到ip地址检查的问题,因此自己写了一个checkIPAdress方法。
 1/**
 2   * check input is ip or not.
 3   * @return a strResult.1 is not ip adress,0 is ipadress
 4   */

 5
 6  private String checkIPAdress(String input) {
 7
 8    String strResult = "0";
 9    //identify the length of the string
10    if (input.length() < 7 || input.length() > 15{
11      return "1";
12    }

13
14    int startIndex = 0;
15    Vector v = new Vector();
16    int index = 0;
17    startIndex = input.indexOf(".");
18    while (startIndex < input.length() && startIndex != -1{
19      String temp = input.substring(index, startIndex);
20      v.addElement(temp);
21      index = startIndex + 1;
22      startIndex = input.indexOf(".", startIndex + 1);
23    }

24    v.addElement(input.substring(index));
25    if (v.size() != 4{
26      return "1";
27    }

28    for (int i = 0; i < v.size(); i++{
29      try {
30        int iTemp = Integer.parseInt( (String) v.elementAt(i));
31
32        if (iTemp < 0 || iTemp > 254{
33
34          return "1";
35        }

36
37      }

38      catch (Exception e) {
39        e.printStackTrace();
40        return "1";
41      }

42
43    }

44
45    return strResult;
46
47  }
     起始在判断分割ip地址应该使用split方法。但是因为我系统中用的是jdk1.2所以采用这个笨方法,如果觉得不爽可以改成split,更方便。
posted on 2005-08-11 12:50 jam 阅读(457) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: