IPADDRESS validation
SOURCE : IPAddressVerification.javaimport 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 }}INPUTOUTPUTthe ip address is: true

0 comments :
Post a Comment