Write a function to check whether an input string is a valid
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots (“.”), e.g.,
Besides, leading zeros in the
IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (“:”). For example, the address
However, we don’t replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example,
Besides, extra leading zeros in the IPv6 is also invalid. For example, the address
Example 1:
Example 2:
Example 3:
Solution
We can validate given string with setup rules for IPv4 and IPv6 IP Address. Code comment will give detail explanation for failure test cases.
Code
0 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 59 60 61 62 63 64 65 66 67 68 |
public class ValidateIP { public static void main(String[] args) { String ipv4 = "128.11.10.1"; System.out.println(ipv4 + " : " + new ValidateIP().validIPAddress(ipv4)); String ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; System.out.println(ipv6 + " : " + new ValidateIP().validIPAddress(ipv6)); } public String validIPAddress(String IP) { if (IP == null || IP.length() == 0) return "Neither"; if (checkIP4(IP)) // Validating as IPv4 IP Address. return "IPv4"; if (checkIP6(IP)) // Validating as IPv6 IP Address. return "IPv6"; return "Neither"; } public boolean checkIP4(String IP) { if (IP.charAt(IP.length() - 1) == '.') //Test case is to check pattern like "1.1.1." return false; String[] numbers = IP.split("\\."); // Split with "." if (numbers == null || numbers.length != 4) // After split array length should be 4. return false; for (String str : numbers) { if (str.length() == 0 || str.length() > 3) // Test case is to check patterns like "1.1..1" or 1.1.1234.1 return false; if (str.length() > 1 && str.charAt(0) == '0') // Test case is to check pattern like "1.1.01.1" return false; int val = 0; for (int i = 0; i < str.length(); i++) { // Prepare a number from string char c = str.charAt(i); if (c < '0' || c > '9') return false; val = val * 10 + (int) (c - '0'); } if (val < 0 || val > 255) // Number should fall between 0 to 255. return false; } return true; } public boolean checkIP6(String IP) { if (IP.charAt(IP.length() - 1) == ':') // Test case is to check pattern like "0000:0000:0000:0000:0000:0000:0000:" return false; String[] numbers = IP.split(":"); if (numbers == null || numbers.length != 8) // After split array length should be 8. return false; for (String str : numbers) { if (str.length() == 0 || str.length() > 4) // Test case is to check patterns like "0000:0000:0000:0000::0000:0000:0000" or "0000:0000:0000:0000:000000:0000:0000:0000" return false; int i = 0; while (i < str.length()) { char c = str.charAt(i++); if ((c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F')) // Character range is 0-9, a-f and A-F. return false; } } return true; } } |
Output
We encourage you to write a comment if you have a better solution or having any doubt on the above topic.