Wednesday, April 24, 2013

Selenium : Reverse each word of a sentence

Reverse each word of a sentence

import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class Reverse_words_sentence {
 public static void main(String[] args)
 {
  String sStr=JOptionPane.showInputDialog("Enter a string");
  String[] sArr=StringUtils.split(sStr, " ");
  String sRes;

  for (int i=0 ;i<=sArr.length-1;i++)
  {
   sArr[i]=StringUtils.reverse(sArr[i]);
  }
  sRes=StringUtils.join(sArr, " ");
  System.out.print(sRes);
 }
}

Selenium :Reverse a String without using builtin Functions

Reverse a String without using builtin Functions

import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class Reverse_str
{
 public static void main(String[] args)
 {
  String sStr=JOptionPane.showInputDialog("Enetr a str");
  String sRes="";
  for (int i=sStr.length() ;i>=0;i--)
  {
   sRes=sRes+StringUtils.mid(sStr, i, 1);
  }
  System.out.print(sRes);
 }


}


Tuesday, April 23, 2013

Selenium : To find the length of the string withouth using "Length " function

 To find the length of the string withouth using "Length " function



import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class Len_of_string
{

 public static void main(String[] args)
 {
  String sStr= JOptionPane.showInputDialog("enter string");
  char[] sArr=sStr.toCharArray();
  System.out.print(sArr.length);
 }

}

Selenium : Extract only digits from a string ( try ....catch )

                                                Extract only digits from a string


import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class extract_digits
{

 public static void main(String[] args)
 {
 String sStr=JOptionPane.showInputDialog("Enter any string with Digit");
 String sRes="";
 int sTemp;

 for (int i=1 ; i<=sStr.length();i++)
 {
   try
   {
     sTemp=Integer.parseInt(StringUtils.mid(sStr, i, 1));//substring(sStr, 1, end)
     sRes=sRes+"\n"+sTemp;
   
   }catch (Exception  e)
   { 
   }
 }

 System.out.println(sRes);

}
}



Friday, April 19, 2013

Selenium : Program To display each Character seperatly from a string


 Program To display each Character seperatly from a string


import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class Display_each_char
{

 public static void main(String[] args)
 {
  String sStr=JOptionPane.showInputDialog("Enetr some data");

  for (int i=0 ;i<= sStr.length();i++)
  {
   System.out.println(StringUtils.mid(sStr, i, 1));
  }
  

 }
}
 

Selenium : To find substring inside a string

To find substring inside a string



import javax.swing.JOptionPane;
public class Substring_in_string
{
 public static void main(String[] args)
 {

  String sData1=JOptionPane.showInputDialog("enter a string data");
  String sData2=JOptionPane.showInputDialog("enter a sub string data");
  if (sData1.contains(sData2) == true)
  {
  System.out.print("The substring exits ");
  }
  else
  {
   System.out.print("The substringdoes not exits ");
  }
 }
}

Thursday, April 18, 2013

Selenium : Find occurence of last "/" in an URL without using string reverse function(built in function)

 

 Find occurence of last "/" in an URL without using string reverse function(built in function)



import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;
public class url_bkslah_nrev
{
 public static void main(String[] args)
 {
  String sUrl=JOptionPane.showInputDialog("enter Url");
  Boolean bFlag=false;
  int i;

  for( i=sUrl.length()-1; i>=0 ; i--)  // Do if true until false
  {
   if (StringUtils.mid(sUrl, i, 1).charAt(0) == "/".charAt(0)) //converting to character
   {
    bFlag=true;
    break;
   }
  }

  if (bFlag=true)
  {
   System.out.print("/ exists at "+(i+1));
  }
  else
  {
   System.out.print("/ not exists");
  }

 }
//1/3/56/'
}

Selenium : Find the last occurence of "/" in the URL

                                    Find the last occurence of "/" in the URL



import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class url_backslash
{

 public static void main(String[] args)
 {
  String sUrl=JOptionPane.showInputDialog("enter url");
  Boolean bFlag=false;
  int i;
  String sReverse=StringUtils.reverse(sUrl);
  for ( i=0 ;i<=sUrl.length()-1;i++)
  {
   //if (Character.toString(sReverse.charAt(i)) ==  "/")  //Convert character to string
   if (sReverse.charAt(i)== "/".charAt(0))
   {
    bFlag=true;
    break;
   }
  }

  if ( bFlag = true )
  {
   System.out.print("the last '/' exists @ "+(sUrl.length()- i));
  }
  else
  {
   System.out.print("/ does not exists");
  }
 }

}
 

Timeout : Interesting Article - Ebola Zaire

Good read



One of my friend after watching some horror,thriller,psycho movie asked to me check out this link.Which I found interesting in the beginning and disgusted at the end but surely not boring.

Not for the faint hearted..

Wednesday, April 17, 2013

Selenium : Display data in right angle triangle with side towards right

Display data in right angle triangle with side towards right


I have used "StringUtils.leftPad"  which is present inside " org.apache.commons.lang3.StringUtils; "

1. Download common/common-lang3.jar.zip( 613 k) from http://www.java2s.com/Code/Jar/c/Downloadcommonlang3jar.htm

2.Extract Jar files and the files to your Package in Eclipse.


 import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;
public class Triangle_right
{
 public static void main(String[] args)
 {
  String sRes="";
  String sData=JOptionPane.showInputDialog("enter data");
  for (int i=1 ; i<=sData.length();i++)
  {
   sRes=sRes+StringUtils.leftPad( sData.substring(0, i), sData.length())+"\n";    
  }
  System.out.print(sRes);
 }
}

Selenium :Display data in form of triangle in reverse

 Display data in form of triangle in reverse



import javax.swing.JOptionPane;

public class display_data_tri2
{

 public static void main(String[] args)
 {
    String sRes="";
    String sData=JOptionPane.showInputDialog("enter data");
    for (int i=sData.length() ;i>=1;i--)
    {
     sRes=sRes+sData.substring(0, i)+"\n";    
    }
    System.out.print(sRes);
  } 
}
 

Selenium : To display data in the form of right angled triangle


 To display data in the form of right angled triangle


import javax.swing.JOptionPane;
public class Triangle_left
{
 public static void main(String[] args)
 {
  String sRes="";
  String sData=JOptionPane.showInputDialog("enter data");
  for (int i=1 ; i<=sData.length();i++)
  {
   sRes=sRes+sData.substring(0, i)+"\n";    
  }
  System.out.print(sRes);
 }
}
 

Selenium :Search a String in a 2D array


Search a String in a 2D array



import javax.swing.JOptionPane;

public class Search_str_arr
{

 public static void main(String[] args)
 {

  String arr_String[][]=new String[3][3];

  for (int i=0 ;i<=2 ;i++)
  {
   for (int j=0;j<=2;j++ )
   {
    arr_String[i][j]=JOptionPane.showInputDialog("Enter string for arr_String["+i+"]["+j+"]");
   }
  }

  String sData=JOptionPane.showInputDialog("Enter Data to be searched");
  Boolean bFlag=false;

  for (int i=0 ;i<=2 ;i++)
  {
    for (int j=0;j<=2;j++ )
    {
     if (arr_String[i][j]==sData)
     {
      bFlag=true;
     }
    }
  
   if (bFlag=true)
   {
    break ;
   }   
  }

  if (bFlag=true)
  {
  System.out.println("the data exists");
  }
  else
  {
   System.out.println("the data does not exist");
  }
   
 }

}

Tuesday, April 16, 2013

Selenium :Transpose of Matrix

                                                    Transpose of Matrix



public class Transpose_Matrix
{
 /**
  * @param args
  */
 public static void main(String[] args)
 {
  String sRes="";
  int aArray[][]=new int [3][3];
  aArray[0][0]=0;
  aArray[0][1]=0;
  aArray[0][2]=0;

  aArray[1][0]=1;
  aArray[1][1]=1;
  aArray[1][2]=1;

  aArray[2][0]=2;
  aArray[2][1]=2;
  aArray[2][2]=2;


  for (int i=0 ; i<=aArray.length-1 ;i++)
  {
   for (int j=0;j<=aArray[0].length-1;j++)
   {
    sRes=sRes+" "+aArray[i][j];
   }
   sRes=sRes+"\n";
  }
  System.out.println(sRes);
  sRes="";
  //-----------------------------------------Method 1 
  for (int j=0;j<=aArray[0].length-1;j++)

  {
   for (int i=0 ; i<=aArray.length-1 ;i++)
   {
    sRes=sRes+" "+aArray[i][j];
   }
   sRes=sRes+"\n";
  }
  System.out.println(sRes);
  sRes="";
  //-----------------------------------------Method 2
  for (int i=0 ; i<=aArray.length-1 ;i++)
  {
   for (int j=0;j<=aArray[0].length-1;j++)
   {
    sRes=sRes+" "+aArray[j][i];
   }
   sRes=sRes+"\n";
  }
  System.out.println(sRes);
 }
}

Selenium :Print Array 3X3 in the form of Matrix

Print Array 3X3  in the form of Matrix



public class Print_array_in_matrix
{
 public static void main(String[] args)
 {
  String sRes="";
  int aArray[][]=new int [3][3];
  aArray[0][0]=0;
  aArray[0][1]=0;
  aArray[0][2]=0;

  aArray[1][0]=0;
  aArray[1][1]=0;
  aArray[1][2]=0;

  aArray[2][0]=0;
  aArray[2][1]=0;
  aArray[2][2]=0;


  for (int i=0 ; i<=aArray.length-1 ;i++)
  {
   for (int j=0;j<=aArray[0].length-1;j++)
   {
    sRes=sRes+" "+aArray[i][j];
   }
   sRes=sRes+"\n";
  }
  System.out.print(sRes);
  
 }
}


Selenium :To find Factorial of a number



To find Factorial of a number



import javax.swing.JOptionPane;
public class Factorial
{
 public static void main(String[] args)
 {
  int iRes=1;
  String sPict="1";
  int ifact=Integer.parseInt(JOptionPane.showInputDialog("enter an integer"));

  for (int i=2 ; i<=ifact ; i++)
  {
   iRes=iRes*i;
   sPict=i+"x"+sPict;
  
  }
  System.out.println(sPict+"="+iRes); 
 }
}

Monday, April 15, 2013

Selenium : Accept Array and print in Reverse order

Array in Reverse order



public class Array_Reverse
{
 public static void main(String[] args)
 {
  int aArray[]= new int[2];//  2 means not 2+1 --> aArray[0] , aArray[1]
  for (int iSize=0 ; iSize<=aArray.length-1 ; iSize++)
  {
   aArray[iSize]=Integer.parseInt(JOptionPane.showInputDialog("Enter value for aArray["+iSize+"]" ));
  }
  //System.out.println(aArray[0]);

  int iSize=aArray.length-1;
  for ( iSize=aArray.length-1 ; iSize>=0 ; iSize--) //"iSize>=0 "Execute if true till false
  {
   System.out.println("aArray["+iSize+"]="+aArray[iSize]);
  }
 
  for ( iSize=0 ; iSize<=aArray.length-1 ;iSize++ )// Execute if true till false
  {
   System.out.println(aArray[iSize]);
  }
 
  System.out.println("Done"); 

 }
}

Selenium : To check whether the given number is prime

To check whether the given number is prime


import javax.swing.JOptionPane;

public class Prime {
 public static void main(String[] args)
 {
  boolean bFlag=true;
  int iVar=Integer.parseInt(JOptionPane.showInputDialog("Enter an integer value"));
  for (int i=2 ; i<=iVar/2 ; i++)
  {
   if (iVar%i == 0)
   {
    System.out.print("The number is not prime");
    bFlag=false;
    break ;
   } 
 
  }
  if( bFlag == true )
  {
   System.out.print("The number is prime");
  }   
 }
}

Selenium : Size of Matrix or 2D array

 Calculate Size of Matrix or Size of 2D Array


public class Size_Matrix {
 public static void main(String[] args)
 {
  int[][] matrix = new int[20][30];
  System.out.println("Number of rows = " + matrix.length);
  System.out.println("Number of columns = " + matrix[0].length);
 }

Selenium : Units of Electricity bill

Calculate Units of Electricity bill

0-50      -> $.5/Unit
50-100  ->$1.5/Unit
100-200->$2 /unit
150-200->$3/Unit
>200    ->  $4/unit


import javax.swing.JOptionPane;

public class Electricity
{
 public static void main(String[] args)
 {
  int iUnit=Integer.parseInt(JOptionPane.showInputDialog("Enter units consumed"));

  if (iUnit<= 50) // ex 30 unit ==$0.5
  {
   System.out.print("Pay ="+"$.5");
 
  }
  else if (50  < iUnit && iUnit <=100 )  //Eg 70 Unit = 30.5
  {
   System.out.print("Pay =$"+(.5+(iUnit-50)*1.5));
  }
  else if (100  < iUnit && iUnit <=150 ) //Eg 120 Unit = 115.5
  {
   System.out.print("Pay =$"+( ((iUnit-100)*2) + ((50*1.5)) +(.5)));
  }
  else if (150  < iUnit && iUnit <=200 ) //Eg 170 Unit =$235.5
  {
   System.out.print("Pay =$"+((iUnit-150)*3+(50*2)+(50*1.5)+.5));
  }
  else if (iUnit >200 )  //Eg 210 units
  {
   System.out.print("Pay =$"+((iUnit-200)*4+(50*3)+(50*2)+(50*1.5)+.5));
  }
 }
}




Saturday, April 13, 2013

Selenium :Connect to an Excel Spreadsheet using JDBC in Java


Connect to an Excel Spreadsheet using JDBC in Java



This tutorial will show you how to connect to and read data from an Excel spreadsheet using JDBC.

To start, we need to setup the local ODBC connection.

Navigate to your computers Control Panel and locate the Administrative Tools.



Once in the Administrative Tools section, locate Data Sources (ODBC)



The ODBC Data Source Administor menu will open



Select the System DSN tab and click Add

Find Driver do Microsoft Excel(*.xls) from the list and click Finish

Give the Data Source Name & Description



Next, click Select Workbook and locate the spreadsheet you wish to use



In this case, we are using worcester.xls. Select it and click OK.

Click OK again to exit the setup. The ODBC connection is now complete.

Now that the ODBC connection is setup, its time for the Java code.


Note :1st Row in Excel is considered as column header 

-------------------------------------------------------------------
import java.sql.*;
public class Connect_Xl_using_JDBC
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:worcester");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from [Sheet1$]");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
System.out.println("Columns="+numberOfColumns);

while (rs.next())
{
for (int i = 1; i <= numberOfColumns; i++)
{
if (i > 1)
System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);

}
System.out.println("");
}
st.close();
con.close();
}
catch (Exception ex)
{
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}
}

------------------------------------------------------



Friday, April 12, 2013

Selenium: Find greatest of 3 nos

 Find the greatest of 3 nos



import javax.swing.JOptionPane;

public class Greatest_of_3_nos
{

 public static void main(String[] args)
 {
  int a,b,c;
  a=Integer.parseInt( JOptionPane.showInputDialog("Enter an value of a"));
  b=Integer.parseInt(JOptionPane.showInputDialog( "Enter an value of b"));
  c=Integer.parseInt(JOptionPane.showInputDialog( "Enter an value of c"));

  if (a > b && a > c)
  {
   System.out.print(a+" is the greatest");
  }
  else if  (c > b && a < c)
  {
   System.out.print(c+" is the greatest");
  }
  else
  {
   System.out.print(b+" is the greatest");
  }

 }
} 

Selenium : To calculate age of a person

                                                    To calculate age of a person



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;

public class Calculate_age
{
 public static void main(String[] args)
 {
  Date oDt=new Date();
  int iYear=Integer.parseInt(JOptionPane.showInputDialog(null, "Enter birth year"));
  JOptionPane.showMessageDialog(null,(oDt.getYear()+1900)- iYear);  
 }
}
 

Selenium : Validate voters age

Program to check the age of user


import javax.swing.JOptionPane;
public class Vote_eligible {
 public static void main(String[] args)
 {
  int iAge=Integer.parseInt(JOptionPane.showInputDialog("enter age"));
  if (iAge>= 18)
  {
   JOptionPane.showMessageDialog(null, "Voter is above or equal to 18 years ,Elibible to vote");
  }
  else
  {
   JOptionPane.showMessageDialog(null, "Voter not eligible to vote");
  }   

 }
}

Selenium:Get Month Name (Using Select Case and Array)

 Display Current Month Name using Array and Select Case




import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class Month_Name
{

 public static void main(String[] args)
 {
  Date dt=new Date();
  System.out.println(dt.getMonth());

  String Month_Name[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

  switch (dt.getMonth())//(c.MONTH+1)
  {
  case 0:
   System.out.println(Month_Name[0]);
   break;
  case 1:
   System.out.println(Month_Name[1]);
   break;
  case 2:
   System.out.println(Month_Name[2]);
   break;
  case 3:
   System.out.println(Month_Name[3]);
   break;
  case 4:
   System.out.println(Month_Name[4]);
   break;
  case 5:
   System.out.println(Month_Name[5]);
   break;
  case 6:
   System.out.println(Month_Name[6]);
   break;
  case 7:
   System.out.println(Month_Name[7]);
   break;
  case 8:
   System.out.println(Month_Name[8]);
   break;
  case 9:
   System.out.println(Month_Name[9]);
   break;
  case 10:
   System.out.println(Month_Name[10]);
   break;
  case 11:
   System.out.println(Month_Name[11]);
   break;
 
  }

 }

}
 

Tuesday, April 9, 2013

Selenium :Extracting Date from input string or putting date in particular format

                           Accepting String calender data and extracting date



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//to create a SimpleDateFormat object and use it to parse Strings to Date and
//to format Dates to Strings. If you've tried SimpleDateFormat and it didn't work, then please show your code and any errors you may receive.
//
//Addendum: "mm" in the format String is not the same as "MM". Use MM for months and mm for minutes. Also, yyyyy is not the same as yyyy.

public class Changing_Date_Format
{

 public static void main(String[] args) throws ParseException
 {
  String date_s = "2011-01-18 00:00:00.0";

        // *** note that it's "yyyy-MM-dd hh:mm:ss" not "yyyy-mm-dd hh:mm:ss" 
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = dt.parse(date_s); //Converting String to Date

        // *** same for the format String below
        SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(dt1.format(date));

 }
}

Selenium : Date

To Demonstrate date function



import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class Month_Name
{

 public static void main(String[] args)
 {
  Date dt = new Date();
  System.out.print(dt.toString());
  System.out.println(dt.getMonth()+":"+dt.getDate()+":"+dt.getYear());

  Calendar c = new GregorianCalendar();
  System.out.println(c.getTime());
  System.out.println(c.DATE+"/"+c.MONTH+"/"+c.YEAR);
 }

}

Selenium : Datatype Conversions

 Datatype Conversions

 //To demonstrate DataType conversions



public class Convert_2_int
{

 public static void main(String[] args)
 {
  String s="123";
  int a=Integer.parseInt(s);
  Float b=Float.parseFloat(s);
  System.out.println(a);
  System.out.println(b);
 }

}

Selenium : Basic arithematic operations and Insert Spaces

Basic arithematic operations



//To demonstrate the use of all arithematic operators
//Inserts 2 spaces   -     String.format("%2s","")

public class All_Arithematic_Operators
{

 public static void main(String[] args)
 {
  System.out.println(1+2);
  System.out.println(1-2);
  System.out.println(1/2);
  System.out.println(1*2);
  System.out.println(Math.sqrt(2));

  int a=1,b=2;
  if (a==1 && b==2 )
  {
   System.out.println("a==1 && b==2"+String.format("%2s","")+"true --a="+a+" b="+b);//Insert spaces
  }
  if (a!=2 || b!=1 )
  {
   System.out.println("a!=2 || b!=1"+String.format("%2s","")+"true --a="+a+" b="+b);//Insert Spaces
  }

  if (a < b)
  {
   System.out.println("a < b"+String.format("%2s","")+"true --a="+a+" b="+b);//Insert Spaces
  }
  if (a <= b)
  {
   System.out.println("a <= b"+String.format("%2s","")+"true --a="+a+" b="+b);//Insert Spaces
  }

 }

}

Thursday, April 4, 2013

Selenium : Get and Set

Selenium : Get and Set


This would probably help

Selenium : Initialising Objects

Initialising Objects

Example to open the browser and click on the Search button .


public class Test
{
 public static void test1() 
 {
  FirefoxDriver oFx=new FirefoxDriver ();   
  EventFiringWebDriver oEvent=new EventFiringWebDriver(oFx);
  oEvent.get("
http://www.google.co.in/");
  Thread.sleep(1000);

  WebElement oButton=oEvent.findElement(By.id("gbqfbb"));  //Object initialising
  oButton.click();
  System.out.print("Fin");

  oEvent.close();
  oEvent.quit();
}
}

Selenium : Verify element - 1. ispresent 2. isvisible 3. isenable 4. textpresent

Selenium : Verify element - 1. ispresent 2. isvisible 3. isenable 4. textpresent


  1. To check Element Present:
    if(driver.findElements(By.xpath("value")).size() != 0){
    System.out.println("Element is Present");
    }else{
    System.out.println("Element is Absent");
    }
    
    Or
    if(driver.findElement(By.xpath("value"))!= null){
    System.out.println("Element is Present");
    }else{
    System.out.println("Element is Absent");
    }
    
  2. To check Visible:
    if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){
    System.out.println("Element is Visible");
    }else{
    System.out.println("Element is InVisible");
    }
    
  3. To check Enable:
    if( driver.findElement(By.cssSelector("a > font")).isEnabled()){
    System.out.println("Element is Enable");
    }else{
    System.out.println("Element is Disabled");
    }
    
  4. To check text present
    if(driver.getPageSource().contains("Text to check")){
    System.out.println("Text is present");
    }else{
    System.out.println("Text is absent");
    }
  5. WebElement rxBtn = driver.findElement(By.className("icon-rx"));
    WebElement otcBtn = driver.findElement(By.className("icon-otc"));
    WebElement herbBtn = driver.findElement(By.className("icon-herb"));
Assert.assertEquals(true, rxBtn.isDisplayed());
Assert.assertEquals(true, otcBtn.isDisplayed());
Assert.assertEquals(true, herbBtn.isDisplayed()); 

6. driver.findelement(By.id("id1")).isDisplayed()
  driver.findelement(By.id("id2")).sendkey("test"); 


Example :
 protected boolean isElementPresent(By by)
 {
   try {
    driver.findElement(by);
    return true;
       }
   catch (NoSuchElementException e)
  {
    return false;
  }
}