IPADDRESS validation

IPADDRESS validation
SOURCE : IPAddressVerification.java

import java.util.StringTokenizer;

public class IPAddressVerification {

int count = 0;

public static void main(String[] args) {

String ip = "1.2.3.8";

IPAddressVerification ipr = new IPAddressVerification();
try {
boolean b = ipr.validate(ip);
System.out.println("the ip address is: " + b);
} catch (Exception e) {
// TODO: handle exception
}

}

private boolean validate(String ip) {

StringTokenizer st = new StringTokenizer(ip, ".");

int token = st.countTokens();

System.out.println(token);
if (token != 4) {

return false;
}

while (st.hasMoreTokens()) {

int i = Integer.parseInt(st.nextToken());

if (i < 0 || i > 255) {

return false;
}

if (i == 0) {

count++;
}

if (count == 4) {

return false;
}

}

return true;
// TODO Auto-generated method stub

}

}


INPUT




OUTPUT

the ip address is: true






Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment