Comparing two strings in Java

package javabasics;

public class compare2String
{

	public static void main(String[] args) 
	{
		
		String S1 = "India is my country";
		String S2 = "USA is my country";
		  
		//Comparing two strings. Return true If both match else return false
		System.out.println("S1 equals to S2? : --- "+S1.equals(S2)+"\n");  
		
	}

}

Output:

S1 equals to S2? : --- false

 

Write and Read from Text File in JAVA

package javabasics;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReadAndWrite {

	public static void main(String[] args) throws IOException {
		
		String TestFile = "E:\\Selenium\\notePadfile.txt";

		File fc = new File(TestFile);

		fc.createNewFile();

		  
		//Writing In to file.
		
		FileWriter fw = new FileWriter(TestFile);

		BufferedWriter bw = new BufferedWriter(fw);

		bw.write("This Is First Line."); //Writing In To File.

		bw.newLine();//To write next string on new line.

		bw.write("This Is Second Line."); //Writing In To File.

		bw.close();
		  
		//Reading from file.
		
		FileReader fr = new FileReader(TestFile);

		BufferedReader br = new BufferedReader(fr);

		String Content = "";
		
		//Loop to read all lines one by one from file and print It.

		while((Content = br.readLine())!= null){

		System.out.println(Content);

		}

	}

}

 

Data Types in JAVA

Data Type: byte

package javabasics;

public class ByteDataType {

	public static void main(String[] args) 
	{		
		byte marks = 50;
				
		System.out.println("Marks scored in maths: --"+marks +"\n"+"\n");		
	}

}

Data Type: boolean

package javabasics;

public class BooleanDataType {

	public static void main(String[] args) 
        {
		
		boolean buttonEnabled = true;
		
		boolean buttonDisabled = false;
		
		if(buttonEnabled==false)
		{
			System.out.println("Button enabled: --"+buttonEnabled);			
		}
		else
		{
			System.out.println("Button disabled: --"+buttonDisabled);	
		}
		
	}

}
Output:

Button disabled: -- flase

Data Type: char

package javabasics;

public class CharDataType {

	public static void main(String[] args) 
	{
		
		char a='Selenium';
		
		char b='Webdriver';
				
		System.out.println("First char value a is: --"+a +"\n"+"\n");	
		
		System.out.println("Second char value a is: --"+b +"\n"+"\n");
		
	}

}

Data Type: short

package javabasics;

public class ShortDataType 
{
	public static void main(String[] args) 
	{		
		short money = 150;
				
		System.out.println("I am having "+money+" rupees in my wallet" +"\n"+"\n");		
	}
}

Data Type: int

package javabasics;

public class IntDataType 
{
	public static void main(String[] args) 
	{		
		int number = 8585855;
				
		System.out.println("My landline number is: ---" +number +"\n"+"\n");
		
	}

}

Data Type: long

package javabasics;

public class LongDataType 
{

	public static void main(String[] args) 
	{
		
		long number = 858585;
				
		System.out.println("My number is: ---" +number +"\n"+"\n");		
				
	}

}

Data Type: float

package javabasics;

public class FloatDataType {

	public static void main(String[] args) {
		
		float totalPercentage = (float) 85.36; // type casting
		
		//float totalPercentage1 = 85.36f;
				
		System.out.println("My percentage is: ---" +totalPercentage +"\n"+"\n");	
		
		
	}

}

Data Type: double

package javabasics;

public class DoubleDataType {

	public static void main(String[] args) {
		
		double totalPercentage =  85.36;		
						
		System.out.println("My percentage is: ---" +totalPercentage +"\n"+"\n");	
	}

}

 

Read Data from CSV using CSVReader

package com.Testing;

import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import au.com.bytecode.opencsv.CSVReader;
import jxl.read.biff.BiffException;

public class ReadDataUsingCSVReader {
	
	public static WebDriver driver;
	
	
  @SuppressWarnings("resource")
@Test
  public void ReadDatafromXLSL() throws InterruptedException, BiffException, IOException {	  
			  
	  System.out.println("----------------------------ReadDatafromXLSL ----------------------------"+"\n" +"\n");	 	  
	  
	  
	// This will load csv file 
	  CSVReader reader = new CSVReader(new FileReader("D:\\QA\\demo.csv"));
	  
	// this will load content into list
	  List<String[]> li=reader.readAll();
	  System.out.println("Total rows which we have is "+li.size());
	            
	 // create Iterator reference
	  Iterator<String[]>i1= li.iterator();
	    
	 // Iterate all values 
	 while(i1.hasNext()){
	     
	 String[] str=i1.next();
	   
	 System.out.print(" Values are ");
	 
	 for(int i=0;i<str.length;i++)
	{
	 
	   System.out.print(" "+str[i]);
	 
	}
	   System.out.println("   ");
	     
	    
	}
	  
	  
	        
      	  	
      
  }
  
 
}

 

Read Data from CSV using FileReader

package com.Testing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import jxl.read.biff.BiffException;

public class ReadDataUsingFileReader {
	
	public static WebDriver driver;
	
	
  @Test
  public void ReadDataUsingFileReader() throws InterruptedException, BiffException, IOException {	  
			  
	  System.out.println("----------------------------ReadDataUsingFileReader ----------------------------"+"\n" +"\n");
	  
	  FileReader fr = new FileReader(new File("D:\\QA\\demo.csv"));
	  
	  BufferedReader br = new BufferedReader(fr);
	  
	  String st;
	  
	  while ((st = br.readLine()) != null)
	  {
		  System.out.println(st);
		  
		  driver = new FirefoxDriver();		
			
		  driver.manage().window().maximize();
			
		  driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		  
		  driver.get("http://test.com");
		  
		  Thread.sleep(5000);
		  
		  driver.findElement(By.name("uid")).sendKeys(st);
		  	   
		  driver.findElement(By.xpath("//a[contains(text(),'Log in')]")).click();
		  
		  Thread.sleep(5000);	 
		  
		  Assert.assertEquals("Executive Summary", "Executive Summary", "Executive Summary");
		  
		  System.out.println("Assert Command on home dashboard Working Fine" +"\n" +"\n");
		  
		  driver.quit();
		  
	  }
	  
	        
      	  	
      
  }
  
 
}

 

Return values in Seleneium Webdriver

package webdriver;

public class ReturnValue {

	public static void main(String[] args) 
	{
		ReturnValue obj = new ReturnValue();
		
		int sum=obj.add(250, 78);
		
		System.out.println("Addition Result: --" +sum);
		
		double result = obj.sub(725.5, 122.3);
		
		System.out.println("Substract Result: --" +result);		
			
	}
	public int add(int a, int b)
	{
		int c = a + b;
		
		return c;
		
	}
	public double sub(double x, double y)
	{
		double result = y-x;
		
		return result;
	}

}

 

1 2 3 4 6