forked from pratyushmp/code_opensource_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateIP.java
More file actions
41 lines (36 loc) · 1.06 KB
/
ValidateIP.java
File metadata and controls
41 lines (36 loc) · 1.06 KB
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
import java.util.Scanner;
public class ValidateIP {
public static void main(String[] args) {
System.out.println("Enter ip address:\n");
Scanner sc = new Scanner(System.in);
String ip = sc.next();
if(isValidIP(ip)) {
System.out.println("This is valid ip address");
} else {
System.out.println("Not a valid ip address");
}
}
static boolean isValidIP (String ip) {
try {
if ( ip == null || ip.isEmpty() ) {
return false;
}
String[] parts = ip.split( "\\." );
if ( parts.length != 4 ) {
return false;
}
for ( String s : parts ) {
int i = Integer.parseInt( s );
if ( (i < 0) || (i > 255) ) {
return false;
}
}
if ( ip.endsWith(".") ) {
return false;
}
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
}