Assuming your scala solution that you wrote in the comment has the following:
def validIPAddress(IP: String): String = {
if (pattenIPv4.matcher(IP).matches()) "IPv4"
if (pattenIPv6.matcher(IP).matches()) "IPv6"
else "Neither"
}
The first if
line will be evaluated but will not return without a return
keyword, so it will fall through the next conditional.
You can fix that in two ways, one is to add return
:
if (pattenIPv4.matcher(IP).matches()) return "IPv4"
or maybe better add an else
to the second line, so you can avoid the return
as the whole thing will be evaluated as a single expression:
def validIPAddress(IP: String): String = {
if (pattenIPv4.matcher(IP).matches()) "IPv4"
else if (pattenIPv6.matcher(IP).matches()) "IPv6"
else "Neither"
}
Also, as a side note, all those var
s can be val
s since you are not mutating them, and it's a good practice in scala to have the guarantee that they will always have the same value.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…