Posts

Showing posts from April, 2017

Shifting Elements in an Array n time

public class ArrayRotation { public static void main(String[] args) { int arr[]={1,2,3}; int length=arr.length;                 // change the  rotation/shift value int rotation=2; int toShift=rotation % length; int end=length; int i=0; int arr2[]=new int[length]; if(toShift!=0){ while(end!=0){ if(toShift>=length) toShift=0; arr2[toShift]=arr[i]; i++; end--; toShift++; } for(int temp:arr2) System.out.print(temp); } else{ for(int temp:arr) System.out.print(temp); } } }

java .gz file reading and writing

compression and decompression of .gz file import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class CompressUncompress { public static void main(String[] args) throws Exception {     //compressing a file to .gz file FileInputStream fis = new FileInputStream("original.txt"); FileOutputStream fos = new FileOutputStream("compressed.gz"); GZIPOutputStream gos = new GZIPOutputStream(fos); Create(fis, gos); //Uncompressing a file to .gz file FileInputStream fis2 = new FileInputStream("compressed.gz"); GZIPInputStream gis = new GZIPInputStream(fis2); FileOutputStream fos2 = new FileOutputStream("uncompressed.txt"); Create(gis, fos2); } public static void Create(InputStream is, OutputStream os) throws Exception { int oneByte; while ((oneByte = is.read()) != -1) { os.w

Java program interface variable recursive call problems

For this problems, Variable value is not initialize. its dependents on anther variable. Problem interface Test{ int a=Test3.c+1; } interface Test2{ int b=Test.a+1; } interface Test3{ int c=Test2.b+1; } public class InhTest{ public static void main(String[] args) { System.out.println(Test.a); System.out.println(Test2.b); System.out.println(Test3.c); } } Same problems with class class Test{ static int a=new Test3().c+1; } class Test2{ static int b=new Test().a+1; } class Test3{ static int c=new Test2().b+1; } public class InhTest{ public static void main(String[] args) { System.out.println(Test.a); System.out.println(Test2.b); System.out.println(Test3.c); } } Output:      3      1      2

404 not found error redirect to a page

Image
                                          tomcat custom http status 404 Error page configuration If a requested page and it does not exist. tomcat default page look like this. Here i am requesting for a page images in my application. But it is not present. Tomcat default error 404 page showing this page. follow below step to override tomcat default 404 error page You can put a custom page. create a the page ' NotFound.html ' in your application home directory. Add the following in web.xml file. ( CATALINA_HOME/conf/web.xml)     <welcome-file-list>         <welcome-file>index.html</welcome-file>         <welcome-file>index.htm</welcome-file>         <welcome-file>index.jsp</welcome-file>     </welcome-file-list>

How to make pendrive bootable

Image
If you CD drive is not working or your laptop doesn't have cd drive. then follow this step to install OS in system via pen drive.  Create a bootable USB flash drive or Install OS for USB drive . 1. Insert your USB (4GB+ preferable) 2. Open Command Prompt As administrator.  3. When the Command Prompt opens, enter the following command:  DISKPART  LIST DISK   SELECT DISK 1 (Once you enter the LIST DISK command, it will show the disk number of your USB drive. In the below image my USB drive disk no is Disk 1. Replace DISK 1 with your disk number)  CLEAN  CREATE PARTITION PRIMARY  SELECT PARTITION 1 

angularjs convert variable value as key in json

Convert string value as key in json object $scope.attVal={"a":"fsdfsdfsd","b":"asdfas","c":"asdfas","d":"","e":"fas","f":"asd","g":"","h":"","i":"","j":"","k":""}; $scope.selectedAttribute="a"; $scope.attrValue="asdda"; $scope.attVal[$scope.selectedAttribute]=$scope.attrValue; In this example you can see, we are converting a string variable into a json key. and Updating old json by key.

Write a program to print K-th repeating element from an array

Printing repeating element from an array import java.util.HashMap; import java.util.Map; public class RepeatingElement { public static void main(String[] args) { int arr[]={1,1,1,2,3,1,6,5,7,7,8,4,4,12,2,5,8,9,7,0,6,0,6,5,3,4,0,0,1}; Map m=new HashMap(); for(int i=0;i<arr.length;i++){ int total=0; for(int j=i+1;j<arr.length;j++){ if(!m.containsKey(arr[i]+"")){ if(arr[i]==arr[j]){ total=total+1; } } } if(total>0){ m.put(arr[i]+"", total+""); } } System.out.println(m); } }

find first repeating letter in a string

print first repeated element public class Array2 { public static void main(String[] args) { /*String s="ffaghglklklh"; Outer:for(int i=0;i<s.length();i++){ Innter: for(int j=i+1;j<s.length();j++){ if(s.charAt(i)==s.charAt(j)){ System.out.println(s.charAt(i)); break Outer; } } }*/ int[] arr={1,2,3,4,5,0,0,1,3}; Outer:for(int i=0;i<arr.length;i++){ Innter: for(int j=i+1;j<arr.length;j++){ if(arr[i]==arr[j]){ System.out.println(arr[i]); break Outer; } } } } }

remove duplicate from array

       Write a program a remove duplicates from array and return a new array. with out using any collection API. import java.util.Arrays; class MyArray<E> { static int length = 0; static int capacity = 10; static Object arr[]; MyArray(){  arr = new Object[capacity]; } public void add(E value) { if (capacity == length) { increaseCapacity(); } arr[length] = value; length++; } public static void increaseCapacity() { capacity=capacity*2; arr=Arrays.copyOf(arr, capacity); }

Arraylist implementation

Similar question:  dynamic array implementation  custom arraylist implementation  own arraylist implementation class MyArray<E> { static int length = 0; static int capacity = 10; static Object arr[]; MyArray(){  arr = new Object[capacity]; } public void add(E value) { if (capacity == length) { increaseCapacity(); } arr[length] = value; length++; } public static void increaseCapacity() { capacity=capacity*2;

java ssl socket example

ssl socket connection with multi threading application.  Server package Test2; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.util.ArrayList; import javax.net.ssl.SSLServerSocket; import javax.net.ssl.SSLServerSocketFactory; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField;

How to install software without administrator rights

Image
You can install software without admin rights follow the step You are working in a organization and If you don't have admin rights don't worry we have a trick. Step Right Click  on executable file , which you want to install. and select  Run Elevated  Application will install in your system with out admin rights.

java 10 features

java jdk 1.10 release features with examples Introducing objects without identity (value types) 64-bit addressable arrays to support large data sets JSR 354: Money and Currency API

java 9 features

java jdk 1.9 release features with examples Java 9 should include better support for multi-gigabyte heaps, better native code integration, and a self-tuning JVM. Key Changes in JDK 9 These changes affect more than one technology area. Java Platform Module System     Introduces a new kind of Java programing component, the module, which is a named, self-describing collection of code and data. This module system: Introduces a new optional phase, link time, which is in-between compile time and run time, during which a set of modules can be assembled and optimized into a custom runtime image; see the jlink tool in Java Platform, Standard Edition Tools Reference. Adds options to the tools javac, jlink, and java where you can specify module paths, which locate definitions of modules. Introduces the modular JAR file, which is a JAR file with a module-info.class file in its root directory. Introduces the JMOD format, which is a packaging format similar to JAR except it can include