就是处理一下字符串
public String solve (String IP) {
// write code here
if (IP == null && IP.length() == 0){
return "Neither";
}
if (IP.contains(":")){
String[] fields = IP.split(":");
if (fields.length != 8){
return "Neither";
}
for (String field : fields) {
if (!isIPV6(field)){
return "Neither";
}
}
return "IPv6";
}else if (IP.contains(".")){
String[] fields = IP.split("\\.");
if (fields.length != 4){
return "Neither";
}
for (String field : fields) {
if (!isIPV4(field)){
return "Neither";
}
}
return "IPv4";
}else {
return "Neither";
}
}
private boolean isIPV6(String str){
if (str.length() > 4 || str.length() == 0){
return false;
}
try {
int max = Integer.valueOf("FFFF",16);
int num = Integer.valueOf(str, 16);
if (num > max){
return false;
}
}catch (NumberFormatException e){
return false;
}
return true;
}
private boolean isIPV4(String str){
if (str.length() == 0 || str.length() > 3){
return false;
}
try {
int n = str.length();
int num = Integer.valueOf(str);
if (num > 255 || num < 0){
return false;
}
if (n == 3 && num < 100){
return false;
}
}catch (NumberFormatException e){
return false;
}
return true;
}
京公网安备 11010502036488号