Java 使用三种方法验证IP地址,但在尝试验证0-255八位字节时中断
我知道做这个项目有更简单的方法。但这不是我们教授希望我们走的路。因此,我需要一些帮助 我试图做的是验证一个IP地址,它有四个部分,三个句点,每个段的值介于0和255之间。还有一个陷阱,我必须有三种方法来做到这一点 首先:验证三个点并分割 秒:确定八位字节的有效数字介于0和255之间 第三个:确定它是一个整数(请注意,由于当前的错误,此方法尚未实现。我非常了解如何处理此问题,因为它基本上是我们的上一个项目。) 当前,当我尝试验证数字范围0-255时,代码会中断。我确实让它用正确数量的数字来验证周期和段,但不是总值 似乎在(字符串f:octet)的“Java 使用三种方法验证IP地址,但在尝试验证0-255八位字节时中断,java,loops,methods,boolean,ip-address,Java,Loops,Methods,Boolean,Ip Address,我知道做这个项目有更简单的方法。但这不是我们教授希望我们走的路。因此,我需要一些帮助 我试图做的是验证一个IP地址,它有四个部分,三个句点,每个段的值介于0和255之间。还有一个陷阱,我必须有三种方法来做到这一点 首先:验证三个点并分割 秒:确定八位字节的有效数字介于0和255之间 第三个:确定它是一个整数(请注意,由于当前的错误,此方法尚未实现。我非常了解如何处理此问题,因为它基本上是我们的上一个项目。) 当前,当我尝试验证数字范围0-255时,代码会中断。我确实让它用正确数量的数字来验证周期
开始处中断。这就像它不会从另一种方法中减去八位组值一样
任何帮助都将不胜感激。提前谢谢你
/*
* 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;
}
}
/*
*文件名:IsAValidIPAddress.java
*项目名称:是有效的IP地址
*
*创造者姓名:约书亚·特里姆
*电邮:jbthype@gmail.com
*课程及组别:CISP 1010
*创建日期:2017年11月10日
*/
导入java.util.Scanner;
/*
*使用三种方法验证IP地址
*
*创建日期:2017年10月9日
*
*@作者约书亚·特里姆
*
*/
公共类地址
{
/*
*方法说明:这是主要方法
*@param args
*@返回无效
*/
公共静态void main(字符串[]args)
{
//扫描器
扫描仪控制台输入=新扫描仪(System.in);
//提示用户
System.out.print(“输入有效的IP地址,例如192.168.1.100:”);
字符串validate=consoleInput.nextLine();
//定义从isAValidIPAddress传递的布尔值
布尔isTrue=isAValidIPAddress(验证);
//while循环
while(isTrue==true)
{
System.out.println(validate+“是一个有效的IP地址,”);
System.out.print(“输入另一个有效的IP地址:”);
validate=consoleInput.nextLine();
}
//告诉用户它是否无效
System.out.print(validate+“不是有效数字,再见!”);
//关闭控制台输入
consoleInput.close();
}
/*
*方法说明:验证字符串是否有三个句点和四个部分
*@param字符串
*@返回布尔值
*/
//是一个有效的IP地址布尔值,它有三个句点,并且只有数字
专用静态布尔ISAValidAddress(字符串ipAddress)
{
//检查八位字节是否为真
布尔isTrue=isavalidocet(ipAddress);
//创建while循环以检查结果是否为真
//定义局部变量
布尔isTrueipAddress=true;
int i1=0;
int length=ipAddress.length();
if(ipAddress.isEmpty())
{
isTrueipAddress=false;
}
如果(!(ipAddress.contains(“.”))
{
//没有发现周期
isTrueipAddress=false;
}
其他的
{
字符串[]段=ipAddress.split(“\\”);
如果(!(segments.length==4))
{
isTrueipAddress=false;
}
}
//字符串moveTest==段();
返回地址;
}
/*
*方法说明:验证每个部分只有4个数字和3个介于0-255之间的值
*@param字符串
*@返回布尔值
*/
//是有效的八位字节布尔值。请确保每个八位字节的数字验证为0-255
专用静态布尔值isAValidOctet(字符串八位字节)
{
布尔值isTrueOctet=true;
int i=0;
int[]验证=新的int[4];
//这里的字符串似乎不想从前面的方法传递。“octet”
for(字符串f:octet)
{
尝试
{
验证[i]=整数.parseInt(f);
if((验证[i]<0)| |(验证[i]>255))
{
isTrueOctet=false;
}
i++;
}
捕获(例外g)
{
isTrueOctet=false;
}
}
返回八位元;
}
}
再次感谢 将代码更改为
String [] segments = ipAddress.split("\\.");
if(!(segments.length == 4))
{
return false;
}
return isAValidOctet(segments);
也在isavalidocet
中更改为
if((validation[i] < 0) || (validation[i] > 255))
{
// isTrueOctet = false;
// may as well return now
return false;
}
谢谢你的帮助,恐怖袋熊!请查看下面的正确编码
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;
}
}
包装编号;
/*
*文件名:IsAValidIPAddress.java
*项目名称:是有效的IP地址
*
*创造者姓名:约书亚·特里姆
*电邮:jbthype@gmail.com
*课程及组别:CISP 1010
*创建日期:2017年11月10日
*/
导入java.util.Scanner;
/*
*使用三种方法验证IP地址
*
*创建日期:2017年10月9日
*
*@作者约书亚·特里姆
*
*/
公共类地址
{
/*
*方法说明:这是主要方法
*@param args
*@返回无效
*/
公共静态void main(字符串[]args)
{
//扫描器
扫描仪控制台输入=新扫描仪(System.in);
//提示用户
System.out.print(“输入有效的IP地址,例如192.168.1.100:”);
字符串validate=consoleInput.nextLine();
//定义从isAValidIPAddress传递的布尔值
布尔isTrue=isAValidIPAddress(验证);
//while循环
//while(isTrue==true)
while(isTrue)//SC-为什么有这个循环?它不进行处理
{
System.out.println(validate+“是一个有效的IP地址,”);
系统
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;
}
}