Wednesday, 15 July 2015

Java Programs Made Easy

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