使用三种方法验证IP地址,但在尝试验证0 - 255个八位字节时中断

时间:2017-10-11 04:38:32

标签: java loops methods boolean ip-address

我知道有更简单的方法来完成这个项目。但这不是我们教授希望我们采取的道路。因此,我需要一些帮助。

我要做的是验证一个IP地址,每个段有四个部分,三个句点和0到255之间的值。并且有一个问题,我必须有三种方法这样做。

首先:验证三个点和细分

:确定八位字节的有效数字介于0到255之间

第三:确定它是一个整数(请注意,由于当前的错误,此方法尚未实现。我非常了解如何处理这个因为它基本上是我们的最后一个项目。

目前代码是在我尝试验证0到255的数字范围时它会中断。我确实让它用正确的数字量但不是总值来验证时段和段。

似乎要打破“for (String f : octet)”开始的位置。它就像它不会从其他方法中携带八位字节值。

非常感谢任何帮助。谢谢你先进!

   /*
 * File name: IsAValidIPAddress.java
 * Project name: Is a Valid IP Address
 * 
 * Creator's name: Joshua Trimm
 * Email: jbthype@gmail.com
 * Course and section: CISP 1010
 * Creation Date: 10-11-17
 */

import java.util.Scanner;
/*
 * <b>Validation of IP Address using three methods</b>
 * <hr>
 * Date created: 10-9-2017
 * <hr>
 * @author Joshua Trimm
 * 
 */
public class IsAValidIPAddress 
{
    /*
     * Method description: This is the main method
     * @param args
     * @return void
     */
    public static void main(String[] args)
    {
        //Scanner
        Scanner consoleInput = new Scanner(System.in);

        //prompt user
        System.out.print("Enter a valid IP address e.g. 192.168.1.100: ");
        String validate = consoleInput.nextLine();

        //define the boolean that pass from isAValidIPAddress
        boolean isTrue = isAValidIPAddress(validate);

        //while loop
        while(isTrue == true)
        {
            System.out.println(validate + " is a valid IP address,");
            System.out.print("Enter another valid IP address: ");
            validate = consoleInput.nextLine();
        }

        //Tell the user if it is invalid
        System.out.print(validate + " is not a valid number, bye!");
        //close the console input
        consoleInput.close();
    }

/*
 * Method description: validates if the string has three periods and there are four sections
 * @param string
 * @return boolean
 */

    //is a valid IP Address boolean and that it has three periods and that it is numbers only
    private static boolean isAValidIPAddress(String ipAddress)
    {

        //check to see if the octet is true
        boolean isTrue = isAValidOctet(ipAddress);

        //Create a while loop to check if results are true

        //define local variables
        boolean isTrueipAddress = true;

        int i1 = 0;

        int length = ipAddress.length();
        if(ipAddress.isEmpty() )
        {
            isTrueipAddress = false;

        }



            if(!(ipAddress.contains(".")))
            {
                //no period was found
                isTrueipAddress = false;

            }
            else
            {
                String [] segments = ipAddress.split("\\.");
                if(!(segments.length == 4))
                {
                    isTrueipAddress = false;

                }


        }
        //String moveTest == segments();
        return isTrueipAddress;


    }

/*
 * Method description: Validate that each section has only 4 three numbers and values between 0 - 255
 * @param string
 * @return boolean
 */     

        // is a valid octet boolean. Make sure the numbers verify 0 - 255 for each octet
    private static boolean isAValidOctet(String octet)
    {

        boolean isTrueOctet = true;
        int i = 0;
        int [] validation = new int[4];


        // here the string doesn't seem two want to pass from the previous method. "octet"
        for (String f : octet)
        {
            try
            {
                validation[i] = Integer.parseInt(f);
                if((validation[i] < 0) || (validation[i] > 255))
                {
                    isTrueOctet = false;
                }
                i++;
            }
            catch (Exception g)
            {
                isTrueOctet = false;
            }
        }

        return isTrueOctet;
    }

}

再次感谢!

2 个答案:

答案 0 :(得分:0)

将代码更改为

String [] segments = ipAddress.split("\\.");
if(!(segments.length == 4))
{
    return false;
}
return isAValidOctet(segments);

同样在isAValidOctet更改为

if((validation[i] < 0) || (validation[i] > 255))
{
    // isTrueOctet = false;
    // may as well return now
    return false;
}

并非真的需要存储到validation - 只需保留在本地变量中

修改

您传入的是String数组,因此您的方法应该是

private static boolean isAValidOctet(String[] octet)

答案 1 :(得分:0)

谢谢@Scary Wombat的帮助!请查看下面的更正编码。

package numbers;

/*
 * File name: IsAValidIPAddress.java
 * Project name: Is a Valid IP Address
 * 
 * Creator's name: Joshua Trimm
 * Email: jbthype@gmail.com
 * Course and section: CISP 1010
 * Creation Date: 10-11-17
 */

import java.util.Scanner;
/*
 * <b>Validation of IP Address using three methods</b>
 * <hr>
 * Date created: 10-9-2017
 * <hr>
 * @author Joshua Trimm
 * 
 */
public class IsAValidIPAddress 
{
    /*
     * Method description: This is the main method
     * @param args
     * @return void
     */
    public static void main(String[] args)
    {
        //Scanner
        Scanner consoleInput = new Scanner(System.in);

        //prompt user
        System.out.print("Enter a valid IP address e.g. 192.168.1.100: ");
        String validate = consoleInput.nextLine();

        //define the boolean that pass from isAValidIPAddress
        boolean isTrue = isAValidIPAddress(validate);

        //while loop
        //while(isTrue == true)
        while(isTrue) // SC - why have this loop at all? it does no processing
        {
            System.out.println(validate + " is a valid IP address,");
            System.out.print("Enter another valid IP address: ");
            validate = consoleInput.nextLine();
            break;
        }

        //Tell the user if it is invalid
        System.out.print(validate + " is not a valid number, bye!");
        //close the console input
        consoleInput.close();
    }

/*
 * Method description: validates if the string has three periods and there are four sections
 * @param string
 * @return boolean
 */

    //is a valid IP Address boolean and that it has three periods and that it is numbers only
    private static boolean isAValidIPAddress(String ipAddress)
    {

        //check to see if the octet is true
        //boolean isTrue = isAValidOctet(ipAddress);

        //Create a while loop to check if results are true

        //define local variables
        boolean isAvalidIPAddress = true;
        String [] segments = ipAddress.split("\\.");
        int i1 = 0;

        // int length = ipAddress.length();  // SC this variable is not used
        if(segments.length != 4 || !(ipAddress.contains(".")) || ipAddress.isEmpty())
        {
            // isAvalidIPAddress = false;
            // SC normally return as soon as possible 
            return false;  // then do not need else statement

        }
        //else
        //{
        return isAValidOctet(segments);
        //}
        //return isAvalidIPAddress;
}
        //String moveTest == segments();





/*
 * Method description: Validate that each section has only 4 three numbers and values between 0 - 255
 * @param string
 * @return boolean
 */     

        // is a valid octet boolean. Make sure the numbers verify 0 - 255 for each octet
    private static boolean isAValidOctet(String[] octet)
    {

        // boolean isTrueOctet = true;
        // int i = 0;
        // SC why use an array ?
        // int [] validation = new int[4];


        // here the string doesn't seem two want to pass from the previous method. "octet"
        for (String f : octet)
        {
            try
            {
                //validation[i] = Integer.parseInt(f);
                int part = Integer.parseInt(f);
                if((part < 0) || (part > 255))
                {
                    //isTrueOctet = false;
                    // SC again retun ASAP
                    return false;
                }
                // i++;
            }
            catch (Exception g)
            {
                //isTrueOctet = false;
                // maybe log exception
                return false;
            }
        }

        //return isTrueOctet;
        return true;
    }

}