集册 Java实例教程 测试以查看给定的地址字符串是否表示文本IPv4地址。

测试以查看给定的地址字符串是否表示文本IPv4地址。

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

634
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
测试以查看给定的地址字符串是否表示文本IPv4地址。

/**

 * Copyright (C) 2009 Nortel, certain elements licensed under a Contributor Agreement.

 * Contributors retain copyright to elements licensed under a Contributor Agreement.

 * Licensed to the User under the LGPL license.

 */

//package com.nowjava;
//时 代 J a v a 公 众 号 - N o w J a v  a . c o m 提供

public class Main {

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

        String address = "nowjava.com";

        System.out.println(isLiteralIPAddress(address));

    }


    /**

     * Test to see if the given address string represents a literal IPv4 address.

     * 

     * @param address

     *           The address string to be tested.

     *           

     * @return

     *           True if literal IPv4 address, False otherwise.

     */

    public static boolean isLiteralIPAddress(String address) {

        String[] octets = address.split("\\.", -1);

        if (octets.length != 4) {

            return false;

        }
/*来 自 N o w J a v a . c o m*/

        for (int i = 0; i < 4; i++) {

            int octetValue = -1;

            try {

                octetValue = Integer.parseInt(octets[i]);

            } catch (NumberFormatException e) {

                
展开阅读全文