Monday, 14 November 2016

How to Check Internet Connection in Java?

Today we'll see how to check internet connectivity using Java. In java we can have several ways to check internet connection. But in our example we'll take ping command to for checking connectivity. We'll just execute the ping command using exec() method, if the exit value of process is 0 (Zero) then we can say internet connection is available else internet connection is not available.

Please note that this is not the only way to check internet connectivity. We can make use of .net package of Java also.

Lets look into the example -

package com.anjan.test;

public class CheckInternetDemo {

    public static String checkInternetConnection(String host) {
      try{
            String cmd = "";
            if(System.getProperty("os.name").startsWith("Windows"))

            {  
               // For Windows OS
               cmd = "ping -n 1 " + host;
            } else {
               // For Other OS - Linux
               cmd = "ping -c 1 " + host;
            }

            Process myProcess = Runtime.getRuntime().exec(cmd);
            myProcess.waitFor();

            if(myProcess.exitValue() == 0) {

                return "Available";
                           
            } else {

                return "Not Available";
            }

       } catch( Exception e ) {

          e.printStackTrace();
          return "Not Available";
       }
    }
   
    public static void main(String args[]){
        System.out.println("Internet Connection : "+
checkInternetConnection("www.google.com"));
    }
}




Output -


Internet Connection : Available

Friday, 11 November 2016

How to deal with enum in Java?

Before dealing with enum, we have questions in mind that what is enum? why and when enum can be used? etc etc.

Enum in java is a datatype that contains fixed set of constants or list of constants. Enum can be used for Months of a year (JANUARY, FEBRUARY...) or Days of a week (SUNDAY, MONDAY...) or Status (ENABLE, DISABLE) etc.

The java enum constants are static and final implicitly. If we are using enum instead of integers (or String), compile-time checking get increased and errors are avoided from passing invalid constants and we document which values are legal to use.

Let us see an example and we'll come to know how to deal with enum -

public class EnumDemo {

enum Status{

Disable(0), Enable(1), Force(2);
 
private final int status;
 
Status(int status){
this.status = status;
}
 
public int getStatus(){
return this.status;
}

}

public static String getNameByCode(int code) {

for (Status e : Status.values()) {
if (code == e.getStatus())
return e.name();
}
return null;
}

public static int getValueByName(String name){
return Status.valueOf(name).getStatus();
}

public static void main(String args[]){

System.out.println("Getting Enum Name passing Enum Value : "+getNameByCode(0));

System.out.println("Getting Enum Value passing Enum Name : "+getValueByName("Disable"));

System.out.println("Getting Ordinal using Enum Directly : "+Status.Enable.ordinal());

System.out.println("Getting Enum Name in String : "+Status.Disable.name());

}

}

Output -

Getting Enum Name passing Ordinal : Disable
Getting Ordinal passing Enum : 0
Getting Ordinal using Enum Directly : 1
Getting Enum Name in String : Disable