import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
import java.util.Scanner ;
/*
IP address is a string in the form "A.B.C.D",
where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed.
Some valid IP address:
000.12.12.034
121.234.12.12
23.45.12.56
Some invalid IP address:
000.12.234.23.23
666.666.23.23
.213.123.23.32
23.45.22.32.
I.Am.not.an.ip
Sample Input
000.12.12.034
121.234.12.12
23.45.12.56
00.12.123.123123.123
122.23
Hello.IP
*/
class Solution{
public static void main
( String [ ] args
) { Scanner in
= new Scanner
( System .
in ) ; while ( in.hasNext ( ) ) {
System .
out .
println ( "Is IP->" + IP
+ " a valid ip : " + isValidIp
( IP
) ) ; }
}
private static boolean isValidIp
( String ipAddress
) { if ( ipAddress == null || ipAddress.isBlank ( ) ) {
return false ;
}
int length = ipAddress.length ( ) ;
if ( length < 7 || length > 15 ) {
return false ;
}
if ( ipAddress.indexOf ( "." ) == - 1 ) {
return false ;
}
String [ ] delimitedString
= ipAddress.
split ( "\\ ." ) ; if ( delimitedString == null || delimitedString.length != 4 ) {
return false ;
}
for ( String s
: delimitedString
) { try {
if ( value < 0 || value > 255 ) {
return false ;
}
}
return false ;
}
}
return true ;
}
}
aW1wb3J0IGphdmEudXRpbC5yZWdleC5NYXRjaGVyOwppbXBvcnQgamF2YS51dGlsLnJlZ2V4LlBhdHRlcm47CmltcG9ydCBqYXZhLnV0aWwuU2Nhbm5lcjsKLyoKSVAgYWRkcmVzcyBpcyBhIHN0cmluZyBpbiB0aGUgZm9ybSAiQS5CLkMuRCIsCndoZXJlIHRoZSB2YWx1ZSBvZiBBLCBCLCBDLCBhbmQgRCBtYXkgcmFuZ2UgZnJvbSAwIHRvIDI1NS4gTGVhZGluZyB6ZXJvcyBhcmUgYWxsb3dlZC4KClNvbWUgdmFsaWQgSVAgYWRkcmVzczoKMDAwLjEyLjEyLjAzNAoxMjEuMjM0LjEyLjEyCjIzLjQ1LjEyLjU2ClNvbWUgaW52YWxpZCBJUCBhZGRyZXNz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