Friday, 17 July 2015

Java Programming

Q)..validate the ip address in the form a.b.c.d
   where a,b,c,d must be between 0and 255
   if validated return 1 else return 2




  1.  
  2. package Set1;
  3. import java.util.*;
  4. public class ClassSet35 {
  5.       public static void main(String[] args) {
  6.             String ipAddress="010.230.110.160";
  7.           boolean b=validateIpAddress(ipAddress);
  8.           if(b==true)
  9.             System.out.println("valid ipAddress");
  10.           else
  11.             System.out.println("not a valid ipAddress");
  12.       }
  13.       public static boolean validateIpAddress(String ipAddress) {
  14.             boolean b1=false;
  15.             StringTokenizer t=new StringTokenizer(ipAddress,".");
  16.             int a=Integer.parseInt(t.nextToken());
  17.             int b=Integer.parseInt(t.nextToken());
  18.             int c=Integer.parseInt(t.nextToken());
  19.             int d=Integer.parseInt(t.nextToken());
  20.             if((a>=0 && a<=255)&&(b>=0 && b<=255)&&(c>=0 && c<=255)&&(d>=0 && d<=255))
  21.                   b1=true;
  22.             return b1;
  23.       }
  24. }