A Java implementation would appear as follows: public String validIPAddress ( String IP ) { if ( isIPv4 ( IP ) ) return "IPv4" ; if ( isIPv6 ( IP ) ) return "IPv6" ; return "Neither" ; } private boolean isIPv4 ( String IP ) { if ( IP . charAt ( 0 ) == '.' || IP . charAt ( IP . length ( ) - 1 ) == '.' ) return false ; String [ ] nums = IP . split ( "\\." ) ; if ( nums . length != 4 ) return false ; for ( String num : nums ) { if ( ! num . matches ( "\\d{1,3}" ) ) return false ; int val = Integer . parseInt ( num ) ; if ( val < 0 || val > 255 || String . valueOf ( val ) . length ( ) != num . length ( ) ) return false ; } return true ; } private boolean isIPv6 ( String IP ) { if ( IP . charAt ( 0 ) == ':' || IP . charAt ( IP . length ( ) - 1 ) == ':' ) return false ; String [ ] nums = IP . split ( ":" ) ; if ( nums . length != 8 ) return false ; for ( String num : nums ) { if ( ! num . matches ( "[\\da-fA-F]{1,4}" ) ) return false ; } return true ; } This Java implementation divides the problem into two smaller functions, isIPv4 and isIPv6 . Then, it constructs the validIPAddress function, which utilizes these helper methods, splitting the input string with regular expressions and returning the corresponding type if the input is valid.