/**
 * Created by IntelliJ IDEA.
 * User: 黄海晏
 * Date: 2004-8-11
 * Time: 11:35:08
 * To change this template use File | Settings | File Templates.
 */
public class IP {
    private long ip;
    private String[] segment;    public IP(long ip) {
        this.ip = ip;
        segment = new String[4];
 
        for (int i = 3; i > -1; i--, ip >>= 8)
            segment[i] = String.valueOf(ip - ((ip >> 8) << 8));
 
    }    public IP(String ipString) throws NumberFormatException,ApplicationException {
        segment = ipString.split("[.]");
        if (segment.length == 4) {
            ip = 0;
            int ipSegment;
            for (int i = 0; i < 4; i++) {
                ip <<= 8;
                ipSegment = Integer.parseInt(segment[i]);
                if (ipSegment > 255 || ipSegment < 0) throw new ApplicationException("ip地址格式不正确!");
                ip += ipSegment;
            }
        } else
            throw new ApplicationException("ip地址格式不正确!");
    }    public String toString() {
        return segment[0] + "." + segment[1] + "." + segment[2] + "." + segment[3];
    }    public long getIp() {
        return ip;
    }    public int compareTo(Object object) {
        IP ip2 = (IP) object;
        if (ip2.getIp() == ip)
            return 0;
        else if (ip > ip2.getIp())
            return 1;
        else
            return -1;
    }}

解决方案 »

  1.   

    /**
     * Created by IntelliJ IDEA.
     * User: 黄海晏
     * Date: 2004-8-11
     * Time: 11:34:12
     * To change this template use File | Settings | File Templates.
     */public class IPMask {
        //private InetAddress IP;
        private IP ip;
        private IP mask;
        private IP ip2;    public IPMask(String ipMask)throws ApplicationException {
            String temp[] = ipMask.split("/");
            if (temp.length == 2) {
                ip = new IP(temp[0]);
                int bit1length = Integer.parseInt(temp[1]);
                if (bit1length < 8 || bit1length > 31) throw new ApplicationException("掩码超过范围!");
                mask = new IP(((1 << bit1length) - 1) << (32 - bit1length));            if ((ip.getIp() &  ~mask.getIp()) != 0) throw new ApplicationException("地址和掩码不匹配!");
                ip2 = new IP(ip.getIp() ^ ~mask.getIp());
            } else
                throw new ApplicationException("ip地址格式不正确!");
        }   /* public IP getIp2() {
            return ip2;
        }
    */
        public IP getIp() {
            return ip;
        }    public String toString() {
            return ip + "-" + ip2;
        } 
        public boolean isInScope(IP inetaddress){
            if(ip.compareTo(inetaddress)>=0 || ip2.compareTo(inetaddress)<=0) return false;
            else return true;
        }
        public static void main(String[] args)throws ApplicationException {
            IPMask mask=new IPMask("195.89.15.0/24");
            System.out.println("mask = " + mask);
        }
    }
      

  2.   

    RESULT:mask = 195.89.15.0-195.89.15.255
      

  3.   

    谢谢你写的程序。
    但我编译你写的IP代码没有通过。错误如下:
    cannot resolve symbol
    public IP(String ipString) throws NumberFormatException,ApplicatioException{
    symbol:clas ApplicationException
    location:class IPcannot resolve symbol
    if (ipSegment > 255 || ipSegment < 0) throw new ApplicationException("ip地址格式不正确!");
    symbol:clas ApplicationException
    location:class IPcannot resolve symbol
    throw new ApplicationException("ip地址格式不正确!");
    symbol:clas ApplicationException
    location:class IP是不是程序需要加载什么类吗?clas ApplicationException在哪的?
      

  4.   

    ApplicationException类按照接口,自己定义一个就可以了。
      

  5.   

    ApplicationException类按照接口,自己定义一个就可以了
    请问楼主是怎样解决的?能写出来参考一下?
    万分感谢!
      

  6.   

    throw new Exception("ip地址格式不正确!");