Posts

Sort the array without changing the position of negative numbers

Problem: Given an array arr[] of N integers, the task is to sort the array without changing  the position of negative numbers (if any) i.e. the negative numbers need not be sorted. Example: Before Sorting : 2 -6 -3 -8 5 -18 0 8 4 6 1 -11 After Sorting : 0 -6 -3 -8 1 -18 2 4 5 6 8 -11         int[] number = {2, -6, -3, -8, 5, -18, 0, 8, 4, 6, 1, -11};         System.out.print("Before Sorting : ");         for(int i=0; i<number.length; i++){             System.out.print(number[i]+" ");          }         System.out.println("");         //Looping over arry to sort the number         for(int i=0; i<number.length-1; i++){             //Swapping two values if first value is grater than the second value.             int k = 0;             for(int j=0;j<number.length-i-1;j++){                 if((number[k] >= number[j+1]) && (number[k] >= 0) && (number[j+1]>=0)){                 number[k] = number[k]+number[j

Print equilateral triangle in java

Below prints Equilateral Triangle. public class PrintEqulateralTriangle {     public static void main(String[] args) {         int numberOfRow = 10;         int numberOfStars = numberOfRow + (numberOfRow - 1);         int median = (numberOfStars+1)/2;         for(int i=1; i<=numberOfRow; i++){             int start = (i==1 ? median : (median - i + 1));             int end = (i==1 ? median : (median + i - 1));             for(int j=1; j<=end;j++){                 if((j>= start) && (j <= end)){                 System.out.print("* ");                 }else{                 System.out.print("  ");                 }             }            System.out.println();         }     } } Output: --------                            *                         * * *                      * * * * *                   * * * * * * *                * * * * * * * * *             * * * * * * * * * * *          * * * * * * * * * * * * *  

Print Right Angle Triagle in Java

Below prints right angle triangle. public class PrintRightAngleTriangle {     public static void main(String[] args) {                for(int i=0; i<=10 ; i++){             for(int j=0;j<=i;j++){                 System.out.print("* ");             }             System.out.println();         }     } } Output: -------- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

Typescipt installation and npm setup in windows

Image
Hi, Here I would like to explain how to install Typescript in windows machine. Before installing Typescript, node.js/npm should be installed in your machine. Follow below steps: 1) Download node.js from this url: https://nodejs.org/en/download/ Note: This download file includes npm also in it. I have downloaded .msi file and the installation is normal process like we install any other application in our windows machine. 2) Once the installation is completed, we can run below commands to check whether node.js and npm is installed properly or not. 3) Go to command prompt and type below commands. 4) Once npm is installed run below command to install typescript. 5) Once Typescript is installed you will see below message. 6) Once Typescript is installed. Run sample typescript file to test whether it is installed properly or not. 7) Create sample file with extention .ts (Helloworld.ts) 8) write simple console.log("Typescript Installed"); 9) Go

Trim Leading and Trailing spaces from InputText on tab out.

Image
Hi, Here I would like to explain one use case to remove Leading and Trailing spaces from input text. Use case: On tab out of input text box, leading and trailing spaces should be removed. To implement this use case I have written custom converter which will do this work. But I need to refresh my component inside the converter code in order to reflect the changes on tab out. There are many use ways to implement this. Below is my way. Create Custom Converter class by implementing javax.faces.convert.Converter import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import org.apache.myfaces.trinidad.context.RequestContext; public class TrimEndSpacesConverter implements Converter{     public TrimEndSpacesConverter() {         super();     }          public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String string)        {             if (string != null) {                 //

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver - Jdeveloper

If anyone experiencing below issue with Jdeveloper. First go and check whether Oracle JDBC jar is added to your class path or not. java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver  at java.net.URLClassLoader$1.run(URLClassLoader.java:202)  at java.security.AccessController.doPrivileged(Native Method)  at java.net.URLClassLoader.findClass(URLClassLoader.java:190)  at java.lang.ClassLoader.loadClass(ClassLoader.java:305)  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)  at java.lang.ClassLoader.loadClass(ClassLoader.java:246)  at java.lang.Class.forName0(Native Method)  at java.lang.Class.forName(Class.java:169)  at webserviceproducer.WebServiceProducer.getConnection(WebServiceProducer.java:34)  at webserviceproducer.WebServiceProducer.main(WebServiceProducer.java:29) I have added 'Oracle JDBC' jar to the project class path and issue got resolved. Thanks for visiting this blog.

Remove duplicates from array without using java utils.

public class RemoveDuplicates { public static void main(String[] args) { Integer arr[] = {1,4,2,1,7,5,0,7,7,1,1,5,4,9,4,1,5,4,9,7,6,1,7,7,8,9,9,0,4}; for(int i=0;i<arr.length-1;i++){ for(int j=i+1;j<arr.length-1;j++){ if(arr[i] == arr[j]){ for(int k = j; k<arr.length-1;k++){ arr[k] = arr[k+1]; }                                         //copy array to remove last element. Integer copy[] = new Integer[arr.length-1]; System.arraycopy(arr, 0, copy, 0, arr.length-1); arr = copy;                                        //decrease j value in case match found to not miss repeated values. j--; } } } for(int z=0; z<arr.length;z++) System.out.print(arr[z]); } }