使用三种方法验证 IP 地址,但在我尝试验证 0 - 255 八位字节时中断
Validation of IP address with three methods but breaks when I try to validate 0 - 255 octet
我知道有更简单的方法来完成这个项目。但这不是我们的教授希望我们走的路。因此,我需要一些帮助。
我试图做的是验证一个 IP 地址,其中包含四个部分、三个句点和每个段的 0 到 255 之间的值。有一个问题,我必须使用 三种方法 才能做到这一点。
首先:验证三点和分割
其次:识别八位位组在0到255之间有一个有效数字
三:识别为整数(请注意,由于目前的bug,该方法尚未实现,我大致明白该怎么做因为它基本上是我们的最后一个项目。)
目前,当我尝试验证数字范围 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;
}
}
再次感谢!
将您的代码更改为
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
- 只需保存在局部变量中
编辑
你传递的是一个字符串数组,所以你的方法应该是
private static boolean isAValidOctet(String[] octet)
感谢@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;
}
}
我知道有更简单的方法来完成这个项目。但这不是我们的教授希望我们走的路。因此,我需要一些帮助。
我试图做的是验证一个 IP 地址,其中包含四个部分、三个句点和每个段的 0 到 255 之间的值。有一个问题,我必须使用 三种方法 才能做到这一点。
首先:验证三点和分割
其次:识别八位位组在0到255之间有一个有效数字
三:识别为整数(请注意,由于目前的bug,该方法尚未实现,我大致明白该怎么做因为它基本上是我们的最后一个项目。)
目前,当我尝试验证数字范围 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;
}
}
再次感谢!
将您的代码更改为
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
- 只需保存在局部变量中
编辑
你传递的是一个字符串数组,所以你的方法应该是
private static boolean isAValidOctet(String[] octet)
感谢@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;
}
}