IP address calculation program using java - Code

Latest

programs

Tuesday, October 11, 2011

IP address calculation program using java



 IP address calculation program using java

IP address calculation program using java

package com.gnec;

import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
 * @author Administrator
 *
 */
public class IPCal {

            /**
             *
             */
            public IPCal() {
                        // TODO Auto-generated constructor stub
            }

            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        String str=null;
                        System.out.println("Enter the decimal number");
                        try{
                                    str=br.readLine();
                                    }
                                    catch(Exception e){
                                                e.printStackTrace();                                        
                                    }
                                    long[ ] a=new long[4];
                                    long  num = Long.parseLong(str);
                                    for (int i=0;i<4;i++){
                                                long  rem = num % 255;
                                                num = num / 255;                                           
                                                a[i]=rem;
                                               
                                                //System.out.println("the rem value in "+i+":"+rem);
                                    }
                                    System.out.print("The ip address is:");
                        for (int j=3;j>=0;j--){
                                    System.out.print(a[j]);
                                    if(j==0)
                                                break;
                                    System.out.print(".");
                        }
            }

}

/**
 *
 */
package com.gnec;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * @author Administrator
 *
 */
public class IPAddressss {

            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        String str=null;
                        StringTokenizer st;
                        int ip[]=new int[4];
                        int i;
                        System.out.println("Enter IPAddress");
                        try{
                        str=br.readLine();
                        }
                        catch(Exception e){
                                    e.printStackTrace();
                                   
                        }
                        st=new StringTokenizer(str,".");
                        i=0;
                        while(st.hasMoreTokens())
                        {
                                    str=st.nextToken();
                                    try{
                                                ip[i]=Integer.parseInt(str);
                                    }
                                    catch(NumberFormatException e)
                                    {
                                                System.out.println ("Enter correct IPAddress");
                                                System.exit(0);
                                    }
                                    i++;
                        }
                        if(i<4)
                        {
                                    System.out.println ("Enter correct IPAddress");
                                    System.exit(0);
                        }
                        for(i=0;i<4;i++)
                        {
                                    if((ip[i]<0)||(ip[i]>255))
                                                            {
                                                System.out.println ("Enter correct IPAddress");
                                                System.exit(0);
                                                            }
                        }
                        if((ip[0]>=0) &&(ip[0]<=127))
                       
                                    System.out.println("ClassA");
                        else if((ip[0]>=128)&& (ip[0]<=191))
                                                System.out.println("ClassB");
                                   
                              else if((ip[0]>=192)&&(ip[0]<=223))

                                                System.out.println("ClassC");
                                        else if((ip[0]>=224)&&(ip[0]<=239))

                                                            System.out.println("ClassD");
                                                  else if((ip[0]>=240)&&(ip[0]<=255))

                                                                        System.out.println("ClassE");
                       
            }}
Expected Output:

Enter the decimal number
1234567891234
The ip address is: 250.24.215.184

Enter IPAddress
191.1.1.1
ClassB

2 comments:

  1. Thanks for sharing such an important post on CIDR Calculator. Your post is very helpful for students to understand CIDR notation to calculate IP Address of Subnet.

    ReplyDelete