-
Notifications
You must be signed in to change notification settings - Fork 0
/
468-ValidateIPaddress.java
58 lines (56 loc) · 1.82 KB
/
468-ValidateIPaddress.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
public String validIPAddress(String IP) {
if(IP.contains(".")){
if(IP.charAt(IP.length()-1)=='.')
return "Neither";
String arr[] = IP.split("\\.");
if(arr.length!=4)
return "Neither";
int flag = 0;
for(String s:arr){
if(s.length()==0)
return "Neither";
if(s.charAt(0)=='0'&&s.length()>1)
return "Neither";
if(s.charAt(0)=='-')
return "Neither";
try{
int no = Integer.parseInt(s);
if(no>=0&&no<=255)
flag =1;
else
return "Neither";
}
catch(Exception e){
return "Neither";
}
}
if(flag==1)
return "IPv4";
}
else if(IP.contains(":")){
String ip6[] = IP.split("\\:");
//int flag1=0;
if(IP.charAt(IP.length()-1)==':')
return "Neither";
if(ip6.length!=8)
return "Neither";
for(String s:ip6){
if(s.length()==0)
return "Neither";
if(s.length()>4)
return "Neither";
for(char c:s.toCharArray()) {
boolean isDigit = c>=48 && c<=57;
boolean isUppercaseAF = c>=65 && c<=70;
boolean isLowerCaseAF = c>=97 && c<=102;
if(!(isDigit || isUppercaseAF || isLowerCaseAF))
return "Neither";
}
}
return "IPv6";
}
else return "Neither";
return "Neither";
}
}