集册 Java实例教程 检查Ip掩码

检查Ip掩码

欢马劈雪     最近更新时间:2020-01-02 10:19:05

477
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
检查Ip掩码


//package com.nowjava;/**来 自 n o w    j a v a  . c o m**/


public class Main {

    public static void main(String[] argv) throws Exception {

        String ip = "nowjava.com";

        System.out.println(checkIpMask(ip));

    }


    public static String checkIpMask(String ip) {

        String ipParts[] = ip.split("\\.");

        if (ipParts.length != 4)

            return "IP address must have 4 parts";

        String message = checkIpMaskPart(ipParts[0]);

        if (message != null)

            return message;

        message = checkIpMaskPart(ipParts[1]);

        if (message != null)

            return message;

        message = checkIpMaskPart(ipParts[2]);/*时 代 J     a    v  a - nowjava.com 提 供*/

        if (message != null)

            return message;

        message = checkIpMaskPart(ipParts[3]);

        if (message != null)

            return message;

        else

            return null;

    }


    private static String checkIpMaskPart(String part) {

        int dash;

        if ("*".equals(part))

            return null;

        dash = part.indexOf('-');

        if (dash == -1) {

            int value = Integer.parseInt(part);

            if (value < 0 || value > 255)

                return (new StringBuilder())

                        .append("Value out of range in '").append(part)

                        .append("'").toString();

            //break MISSING_BLOCK_LABEL_219;

        }

        int from;

        from = Integer.parseInt(part.substring(0, dash));

        if (from < 0 || from > 255)

            return (new StringBuilder())

                    .append("'From' value out of range in '").append(part)

                    .append("'").toString();

        int to;

        try {

            to = Integer.parseInt(part.substring(dash + 1));

            if (to < 0 || to > 255)

                return (new StringBuilder())

                        .append("'To' value out of range in '")

                        .append(part).append("'").toString();

        } catch (NumberFormatException e) {

            
展开阅读全文