Sunday, 7 May 2017

Java program to Merge two Excel sheets

This program has been taken from the below link and enhanced for easy use and easy understanding. This program is working 100% and the result is accurate.
Jars used: poi.jar

https://coderanch.com/t/614715/java/merge-excel-files - Thanks for the program

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;


@SuppressWarnings("deprecation")
public class MSExcel {
 public static void main(String args[]) throws FileNotFoundException{
  MSExcel mslobj = new MSExcel();
  mslobj.readExcel();
 }

 private void readExcel() {
  // TODO Auto-generated method stub
  try {
   String FilePath = "C:\\Users\\test.xls";
   String FilePath1 = "C:\\Users\\test1.xls";
   FileInputStream fis = new FileInputStream(FilePath);
   FileInputStream fis1 = new FileInputStream(FilePath1);
   HSSFWorkbook workbook = new HSSFWorkbook(fis);
   HSSFSheet sheet = workbook.getSheet(“sheet”);
 
   HSSFWorkbook wb = new HSSFWorkbook(fis1);
   //Workbook wb = new XSSFWorkbook();
   HSSFSheet sheet1 = wb.getSheet(“sheet1”);
   copySheets(sheet1, sheet);
 
   FileOutputStream outfile = new FileOutputStream("C:\\Users\\test1.xls");
   wb.write(outfile);
   outfile.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
  public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet){
    copySheets(newSheet, sheet, true);
  }
     
       public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet, boolean copyStyle){
           int maxColumnNum = 0;
           Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>() : null;
           for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
               HSSFRow srcRow = sheet.getRow(i);
               HSSFRow destRow = newSheet.createRow(i);
               if (srcRow != null) {
                   MSExcel.copyRow(sheet, newSheet, srcRow, destRow, styleMap);
                   if (srcRow.getLastCellNum() > maxColumnNum) {
                       maxColumnNum = srcRow.getLastCellNum();
                   }
               }
           }
           for (int i = 0; i <= maxColumnNum; i++) {
               newSheet.setColumnWidth(i, sheet.getColumnWidth(i));
           }
       }
 
       public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet, HSSFRow srcRow, HSSFRow destRow, Map<Integer, HSSFCellStyle> styleMap) {
           @SuppressWarnings("deprecation")
     Set<CellRangeAddress> mergedRegions = new TreeSet<CellRangeAddress>();
           destRow.setHeight(srcRow.getHeight());
           for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
               HSSFCell oldCell = srcRow.getCell(j);
               HSSFCell newCell = destRow.getCell(j);
               if (oldCell != null) {
                   if (newCell == null) {
                       newCell = destRow.createCell(j);
                   }
                   copyCell(oldCell, newCell, styleMap);
                   CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(), (short)oldCell.getColumnIndex());
                   if (mergedRegion != null) {
                       CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastRow(), mergedRegion.getLastColumn());
                       if (isNewMergedRegion(newMergedRegion, mergedRegions)) {
                           mergedRegions.add(newMergedRegion);
                           destSheet.addMergedRegion(newMergedRegion);
                       }
                   }
               }
           }
         
       }
     
       public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {
           if(styleMap != null) {
               if(oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()){
                   newCell.setCellStyle(oldCell.getCellStyle());
               } else{
                   int stHashCode = oldCell.getCellStyle().hashCode();
                   HSSFCellStyle newCellStyle = styleMap.get(stHashCode);
                   if(newCellStyle == null){
                       newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                       newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                       styleMap.put(stHashCode, newCellStyle);
                   }
                   newCell.setCellStyle(newCellStyle);
               }
           }
           switch(oldCell.getCellType()) {
               case HSSFCell.CELL_TYPE_STRING:
                   newCell.setCellValue(oldCell.getStringCellValue());
                   break;
               case HSSFCell.CELL_TYPE_NUMERIC:
                   newCell.setCellValue(oldCell.getNumericCellValue());
                   break;
               case HSSFCell.CELL_TYPE_BLANK:
                   newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                   break;
               case HSSFCell.CELL_TYPE_BOOLEAN:
                   newCell.setCellValue(oldCell.getBooleanCellValue());
                   break;
               case HSSFCell.CELL_TYPE_ERROR:
                   newCell.setCellErrorValue(oldCell.getErrorCellValue());
                   break;
               case HSSFCell.CELL_TYPE_FORMULA:
                   newCell.setCellFormula(oldCell.getCellFormula());
                   break;
               default:
                   break;
           }
         
       }
     
       public static CellRangeAddress getMergedRegion(HSSFSheet sheet, int rowNum, short cellNum) {
           for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
               CellRangeAddress merged = (CellRangeAddress) sheet.getMergedRegion(i);
               if (merged.isInRange(rowNum, cellNum)) {
                   return merged;
               }
           }
           return null;
       }
 
       private static boolean isNewMergedRegion(CellRangeAddress newMergedRegion, Collection<CellRangeAddress> mergedRegions) {
           return !mergedRegions.contains(newMergedRegion);
       }
     
 }

Saturday, 1 April 2017

Simple Java program to find Prime number

This program has made simple for better understanding of beginners.

import java.util.Scanner;


public class PrimeNumber {
public static void main(String args[]){
System.out.println("Enter the number: ");
//To receive input from users
Scanner s = new Scanner(System.in);
int num = s.nextInt();
s.close();
//Logic to detect the prime number
for(int i=2; i<num; i++){
int temp = num%i;
if (temp == 0){
System.out.println("This is not a prime number.");
break;
} 
else{
if (i == (num-1)){
System.out.println("This is a prime number.");
}
}
} 
}


}

Output:
Enter the number: 
21
This is not a prime number.

Enter the number: 
3
This is a prime number.

Simple Java program for Odd or Even

This program has been simplified for beginners to find the given number is Odd or Even.

import java.util.Scanner;


public class OddrEven {
public static void main(String args[]){
//To get the input number from user
System.out.println("Enter the number: ");
Scanner sc = new Scanner(System.in);
int inp = sc.nextInt();
sc.close();
//Simple logic to check the number is Odd or Even
int temp = inp % 2;
if (temp == 0){
System.out.println("This is a even number.");
}else{
System.out.println("This is a odd number.");
}
}

}

Output:
Enter the number: 
45
This is a odd number.

Enter the number: 
22
This is a even number.

Friday, 31 March 2017

Amazon interview question - To find the first repeated character in a String

Question: How to find the first repeated character in a String?

The below java program has been written in easy way to make the beginners understand how easy the way of coding is and how easily a concept can be achieved through programming.

import java.util.Scanner;


public class StrRepeat {
public static void main(String args[]){
//Get the string to check the non repreated character
System.out.println("Enter the String: ");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
char fin = checkLetter(str);
System.out.println("The character is: " + fin);
s.close();
}

//To check each individual char for non repeated
private static char checkLetter(String str) {
// TODO Auto-generated method stub
//To split the string to character
char[] ary = str.toCharArray();
char val = 0;
//To get the length of character
int len = ary.length;
char temp[] = new char[len];
for (int i=0; i<len; i++){
for (int j=0; j<len; j++){
if (j!= i){
if(ary[i] == ary[j]){
temp[i] = '$';
}
}
}
}
for (int k = 0; k< len; k++){
if (temp[k] == '$' ){
val = ary[k];
break;
}
}
return val;
}


}

Output:
Enter the String: lmadam
The character is: m

Amazon interview question - Find first non repeated character in a string

Question: how to find the first non-repeated character in a String?

The below Java program has been optimised and written easy for understanding.

import java.util.Scanner;


public class StrRepeat {
           public static void main(String args[]){
          //Get the string to check the non repreated character
          System.out.println("Enter the String: ");
          Scanner s = new Scanner(System.in);
          String str = s.nextLine();
          char fin = checkLetter(str);
          System.out.println("The character is: " + fin);
          s.close();
}

//To check each individual char for non repeated
private static char checkLetter(String str) {
// TODO Auto-generated method stub
           //To split the string to character
           char[] ary = str.toCharArray(); 
           char val = 0;
           //To get the length of character
           int len = ary.length;
           char temp[] = new char[len];
           for (int i=0; i<len; i++){
                 for (int j=0; j<len; j++){
                    if (j!= i){
                         if(ary[i] == ary[j]){
                                   temp[i] = '$';
                         }
                   }
            }
}
for (int k = 0; k< len; k++){
if (temp[k] != '$' ){
val = ary[k];
break;
}
}
return val;
}


}

Output:
Enter the String: madam
The character is: d

Simple Java program for Palindrome

This program is simplified for easy understanding using the StringBuilder with reverse function in java.

import java.util.Scanner;


public class Palindrome {
public static void main(String args[]){
//Get the string to check 
System.out.println("Enter the string: ");
Scanner sc = new Scanner(System.in);
String inp = sc.nextLine();
sc.close();
//String to string builder to store in heap
StringBuilder sb = new StringBuilder();
sb.append(inp);
//Reverse function over the string 
sb = sb.reverse();
String temp = sb.toString();
//compare to check Palindrome
if (inp.equals(temp)){
System.out.println("The given string is a Palindrome.");
}else{
System.out.println("The given string is not a Palindrome.");
}
}


}

Output:
Enter the string: madam
The given string is a Palindrome.
Enter the string: Rajesh
The given string is not a Palindrome.

Sunday, 23 October 2016

Cobol - DB2 bind process

Precompiler:


The DB2 Precompiler splits the program into two parts: a COBOL and a DB2 part. The embedded SQL is stripped out of the program and put into a partitioned data set (PDS) member, called a DBRM. Just as the COBOL part has to be compiled, the DBRM part has to go through BIND process to create the run-time executable code for the DB2 portion of the COBOL program. To help the COBOL and DB2 part to find each other later at run time, the precompiler engraves each with identical timestamps called
consistency tokens. 


Bind plan:
You can BIND the DBRM into a PLAN (the old way), or you can BIND the instructions into a PACKAGE.


Plan:
A PLAN is an executable module containing the
access path logic produced by the DB2 optimizer. The DBRMs of more than one program or PACKAGES can be bound into a PLAN.


Package:
A PACKAGE is a single, bound DBRM with optimized access paths. The DBRM of a single program is bound into a PACKAGE. To execute a PACKAGE, it should be included in the package list of a PLAN. PACKAGEs are not directly executed, they are only indirectly executed when the PLAN in which they are contained executes.

DBRM & Plan:
The relationship between a
DBRM and a PLAN is one-to-many, the relationship between a DBRM and a PACKAGE is always one-to-one. 


Bind process:
As the number of DBRMs bound to a PLAN increases, binding the DBRM into a PLAN is not recommended. If we need to precompile and bind a new program or one of the programs changes and it is to be precompiled and bound again, all the programs (not just the modified/added program) will be rebound into the PLAN again. Then the BIND process could take hours to complete.

On the other hand, if a DBRM is bound to a PACKAGE and if the program is modified, only that PACKAGE would have to be rebound.


Collection:
A collection is simply a way of grouping PACKAGEs into meaningful groups. You could use COLLECTIONs to separate programs for different application areas, such as payroll and inventory. Another use might be to have customized set of BIND parameters associated with different COLLECTIONs.


Run Time:
At run time, the load module starts up and eventually hits a paragraph containing a CALL to DB2. Then the COLLECTIONs named in the PLAN are searched for the PACKAGE with the same name and consistency token. If you don't find it anywhere in DB2, you get an
-805 error. If you're using the older technique of binding DBRMs directly into PLANs, then an unsuccessful search will result in an -818 error code.

Expense Handler Application with advance technologies

Budget Planner  One of the application developed with Ionic 4 and Python-Flask.  Ionic 4 code:  https://github.com/logeshbuiltin/Expense...